最后一个样例没过

P1104 生日

KARA_214 @ 2024-08-26 19:46:32

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

烦请解答


by dream_dad @ 2024-08-26 19:48:49

@KARA_214 sort不稳定


by King_and_Grey @ 2024-08-26 19:51:33

@KARA_214 把 sort 换成 stable_sort 即可


by King_and_Grey @ 2024-08-26 19:52:49

WA


by King_and_Grey @ 2024-08-26 19:54:10

@KARA_214 看看我的,怀疑你的比较部分有问题

#include <bits/stdc++.h>
using namespace std;
struct node {
    string name;
    int year;
    int month;
    int day;
    int level;
} nn[120];
bool cmp(node a,node b) {
    if(a.year != b.year)
        return a.year < b.year;
    else {
        if(a.month != b.month)
            return a.month < b.month;
        else if(a.day == b.day && a.month == b.month) 
            return a.level > b.level;
        else if(a.day != b.day && a.month == b.month) 
            return a.day < b.day;
    }
}
int main() {
    int n;
    cin>>n;
    for(int i = 1; i <= n; ++i) {
        cin>>nn[i].name>>nn[i].year>>nn[i].month>>nn[i].day;
        nn[i].level = i;
    }
    stable_sort(nn+1,nn+1+n,cmp);
    for(int i = 1; i <= n; ++i) {
        cout<<nn[i].name<<endl;
    }
    return 0;
}

by KARA_214 @ 2024-08-27 08:45:56

@King_and_Grey 谢谢大佬,已AC


|