求助,84分

P1104 生日

wang0728 @ 2023-12-09 16:31:42

#include <bits/stdc++.h>
using namespace std;
struct f{
    string ming;
    int nian,yue,ri;
}a[105];
int n;
int cmp(f xx,f yy){
    if(xx.nian==yy.nian && xx.yue==yy.yue && xx.ri==yy.ri){
        return 1;
    }
    if(xx.nian!=yy.nian) return xx.nian<yy.nian;
    else{
        if(xx.yue==yy.yue) return xx.ri<yy.ri;
        else return xx.yue<yy.yue;
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].ming>>a[i].nian>>a[i].yue>>a[i].ri; 
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++){
        cout<<a[i].ming<<"\n";
    }
    return 0;
}

by LeoYangcz @ 2023-12-10 20:46:01

你这个cmp函数写得有点乱,思路有点太复杂了

下面是我的AC代码,供参考:
#include<bits/stdc++.h>
using namespace std;
struct stu{
    string name;
    int y;
    int m;
    int d;
    int s;
}a[105];
bool cmp(stu a,stu b){
    if(a.y!=b.y){
        return a.y<b.y;
    }
    if(a.m!=b.m){
        return a.m<b.m;
    }
    if(a.d!=b.d){
        return a.d<b.d;
    }
    return a.s>b.s;
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i].name;
        cin>>a[i].y;
        cin>>a[i].m;
        cin>>a[i].d;
        a[i].s=i;
    }
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++){
        cout<<a[i].name<<endl;
    }
    return 0;
}
求关 qwq

by LeoYangcz @ 2023-12-10 20:52:07


cmp函数里没必要用if嵌套

by wang0728 @ 2024-02-23 16:46:50

@LeoYangcz ok


|