wa2求助

P1104 生日

Greeper @ 2024-08-15 17:31:39

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

by suxiaozhou @ 2024-08-15 17:55:36

第17行

return x.id<y.id改为return x.id>y.id

题目中有说了

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


by zhengyuxiang975 @ 2024-08-24 11:16:51

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

|