求大神解答,样例过了,但满面红光

P1104 生日

__LIAM_olivers__ @ 2024-08-23 18:02:38

#include<bits/stdc++.h>
using namespace std;
struct people{
    string s[105];
    int y[105],m[105],d[105];
}a;
int pd(int a1,int a2,int a3,int b1,int b2,int b3){
    if(a1>a2)return 1;
    else if(a1==a2){
        if(a2>b2)return 1;
        else if(a2==b2)
        if(a3>b3){
            if(a3==b3)return 1;
        } 
    }
    return 0;
}
int main(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a.s[i];
        scanf("%d %d %d",&a.y[i],&a.m[i],&a.d[i]);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(pd(a.y[i],a.m[i],a.d[i],a.y[j],a.m[j],a.d[j])){
                swap(a.s[i],a.s[j]);
                swap(a.y[i],a.y[j]);
                swap(a.m[i],a.m[j]);
                swap(a.d[i],a.d[j]);
            }

        }
    }
    for(int i=0;i<n;i++){
        cout<<a.s[i]<<endl; 
    }
    return 0;
}

若有帮助,万分感谢


by AAAuc03 @ 2024-08-23 18:09:14

为甚不不用sort?


by AAAuc03 @ 2024-08-23 18:17:51

#include<bits/stdc++.h>
using namespace std;
struct people{
    string s;
    int y,m,d,l;
}a[105];
bool cmp(people a1,people b){
    if(a1.y< b.y){
        return true;
    }
    else if(a1.y==b.y&&a1.m<b.m) {
        return true;
    }
    else if(a1.y==b.y&&a1.m==b.m&&a1.d<b.d){
        return true;
    }
    else if(a1.y==b.y&&a1.m==b.m&&a1.d==b.d&&a1.l>b.l) {
        return true;
    } 
    return false;
}
int main(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i].s;
        scanf("%d %d %d",&a[i].y,&a[i].m,&a[i].d);
        a[i].l=i;
    }
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++){
        cout<<a[i].s<<endl; 
    }
    return 0;
}

|