80分求助

P1104 生日

Eagle2012 @ 2023-11-24 10:14:48

#include<bits/stdc++.h>
using namespace std;

struct s
{
    string name;
    int n;
    int y;
    int r;

};

bool cmp(s s1, s s2){
    if(s1.n != s2.n){
        return s1.n < s2.n;
    }
    if(s1.y != s2.y){
        return s1.y < s2.y;
    }
    if(s1.r != s2.r){
        return s1.r < s2.r;
    }
    return false;
}
int main(){
    int n;
    cin>>n;
    s a[n];
    for(int i = 0; i < n; i++)
    {
        cin>>a[i].name>>a[i].n>>a[i].y>>a[i].r;
    }

    sort(a,a+n,cmp);

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

by bcbgszyzh @ 2023-11-24 10:42:42

我看看


by bcbgszyzh @ 2023-11-24 10:43:46

如果有两个同学生日相同,输入靠后的同学先输出


by bcbgszyzh @ 2023-11-24 10:44:16

可以写成:

#include<bits/stdc++.h>//万能头 
using namespace std;
struct STU{
    string s;
    int y,m,d,bh;
};
bool cmp(STU a,STU b){
    if(a.y<b.y)return true;
    else if(a.y==b.y){
        if(a.m<b.m)return true;
        else if(a.m==b.m){
            if(a.d<b.d)return true;
            else if(a.d==b.d){
                return a.bh<b.bh;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }else return false;
}
STU p[110];
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>p[i].s>>p[i].y>>p[i].m>>p[i].d;
        p[i].bh=n-i+1;
    }
    sort(p,p+n,cmp);//三个参数,cmp代表排序规则 
//  for(int i=0;i<n;i++){
//      cout<<p[i].s<<" "<<p[i].y<<" "<<p[i].m<<" "<<p[i].d<<" "<<p[i].bh<<'\n';
//  } 
    for(int i=0;i<n;i++){
        cout<<p[i].s<<endl;
    } 
    return 0;
}

by Eagle2012 @ 2023-11-26 16:27:33

@bcbgszyzh 谢谢


|