80分,求调

P1104 生日

aszxqw @ 2024-10-12 13:40:21

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

by ccch1ennn @ 2024-10-12 14:15:20

题目中说有生日相同的情况,后输入的先输出,所以加一个顺序标记

for(int i=1;i<=n;i++){
        cin>>a[i].name>>a[i].y>>a[i].m>>a[i].d;
   ***     a[i].num=i;   ***
    }

cmp函数中最后加一个判断

if(xx.num>yy.num)return 1;
    else return 0;

by ycz1234 @ 2024-10-13 16:42:04

**** ######


|