为什么?样例明明过了呀。

P1104 生日

lizhuorong @ 2023-11-04 13:15:04

#include<bits/stdc++.h>
using namespace std;
struct num{
    string name;
    int year,month,day;
}people[10000];
int b[10000];
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>people[i].name;
        cin>>people[i].year>>people[i].month>>people[i].day;
        b[i]=people[i].year*10000+people[i].month*100+people[i].day;
    }
    int k;
    for(int i=0;i<n;i++){
        k=i;
        for(int j=i+1;j<n;j++){
            if(b[k]<=b[j]){
                k=j;
            }
        }
        if(k!=i)swap(people[i].name,people[k].name);
    }
    for(int i=n-1;i>=0;i--){
        cout<<people[i].name<<endl;
    }
}

by Tsumiki_Mikan @ 2023-11-04 13:39:19

@lizhuorong 这什么排序,没见过,sort不好?


by Tsumiki_Mikan @ 2023-11-04 13:52:44

不用这么麻烦,用结构体排序就好了

#include<bits/stdc++.h>
using namespace std;
struct fuck_ccf{
    long long y,m,d,n;
    string name;
}a[105];
bool cmp(fuck_ccf x,fuck_ccf y){
    if(x.y==y.y&&x.m==y.m&&x.d==y.d) return x.n>y.n;
    if(x.y==y.y&&x.m==y.m) return x.d<y.d;
    if(x.y==y.y) return x.m<y.m;
    return x.y<y.y;
}
int n;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].name>>a[i].y>>a[i].m>>a[i].d;
        a[i].n=i;
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++){
        cout<<a[i].name<<endl;
    }
}

by Tsumiki_Mikan @ 2023-11-04 14:01:18

@lizhuorong 你这个排序不成立,只交换了名字,而且每次找出的都是最后一个比b[i]大的年龄,建议sort,最方便,但数据比较小,用冒泡排序也行


by lizhuorong @ 2023-11-04 14:12:20

@Emily_qwq OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK !
谢谢大佬


by Tsumiki_Mikan @ 2023-11-04 22:23:24

@lizhuorong 别大佬辣我就一蒟蒻

互关嘛

验证码97md


|