16分求助

P1104 生日

yanshuai666 @ 2024-11-23 10:54:07

#include <bits/stdc++.h>
using namespace std;
const int N = 101001;
struct student
{
    string name;
    int num;
    int y;
    int m;
    int d;
} stu[N];
bool cmp(student a, student b)
{
    if (a.y < b.y)
        return true;
    else if (a.y > b.y)
        return false;
    else
    {
        if (a.m < b.m)
            return true;
        else if (a.m > a.m)
            return false;
        else
        {
            if (a.d < b.d)
                return true;
            else if (a.d > b.d)
                return false;
            else
            {
                if (a.num > b.num)
                    return true;
                else
                    return false;
            }
        }
    }
}
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> stu[i].name >> stu[i].y >> stu[i].m >> stu[i].d;
        stu[i].num = i;
    }
    sort(stu, stu + n, cmp);
    for (int i = 0; i < n; i++)
        cout << stu[i].name << endl;

    return 0;
}

by yise @ 2024-11-25 20:30:56

@yanshuai666 cmp的原因


by yise @ 2024-11-25 20:31:28

@yanshuai666

int cmp(student e1,student e2){
    if(e1.y==e2.y){
        if(e1.m==e2.m){
            if(e1.d==e2.d){
                return e1.num>e2.num;
            }else{
                return e1.d<e2.d;
            }
        }else{
            return e1.m<e2.m;
        }
    }else{
        return e1.y<e2.y;
    }
}

|