求掉

P1104 生日

ht__QAQ__ @ 2023-06-17 16:15:43

#include<bits/stdc++.h>
using namespace std;
struct QwQ{
    string name;
    int n,y,r,c,sum;
}a[100001];
int cmp(QwQ x,QwQ y){
    if(x.n<y.n)return 1;
    if(x.n>y.n)return 0;
    if(x.y>y.y)return 1;
    if(x.y<y.y)return 0;
    if(x.r>y.r)return 1;
    if(x.r<y.y)return 0;
    if(x.sum<y.sum)return 0;
    return 1;
}
int m;
int main(){
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>a[i].name>>a[i].n>>a[i].y>>a[i].r;
        a[i].c=2021-a[i].n;
        a[i].sum=i;
    }
    sort(a,a+m,cmp);
    for(int i=0;i<m;i++){
        cout<<a[i].name<<"\n";
    }
    return 0;
}

by dingronghan01 @ 2023-06-17 16:34:15

@zzzhhhzzz 我们是否能把年月日拼起来呢?


by dingronghan01 @ 2023-06-17 16:34:59

然后直接排序


by dingronghan01 @ 2023-06-17 16:40:43

你用快速排序,但是快速排序是不稳定的,所以16分


by dingronghan01 @ 2023-06-17 16:41:10

@zzzhhhzzz 建议用选择排序


by dingronghan01 @ 2023-06-17 16:45:42

#include<bits/stdc++.h>
using namespace std;
struct node{
    string name;
    int year,month,day,date;
}a[105];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].name>>a[i].year>>a[i].month>>a[i].day;
        a[i].date=a[i].year*10000+a[i].month*100+a[i].day;//拼接日期
    }
    for(int i=1;i<n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(a[i].date>=a[j].date)
            {
                swap(a[i],a[j]);
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        cout<<a[i].name<<'\n';
    }
    return 0;
}

by ht__QAQ__ @ 2023-06-17 16:47:58

@dingronghan01 谢谢大佬我标题打错字了


by dingronghan01 @ 2023-06-17 16:52:11

@zzzhhhzzz 不有谢


|