最后一个测试点没过QWQ

P1104 生日

aguo_nuts @ 2024-08-28 21:00:32

#include<bits/stdc++.h>
using namespace std;
struct nb {
    string name;
    int nian;
    int yue;
    int ri;
} a[100000];
bool cmp(nb n1,nb n2) {
    if(n1.nian<n2.nian||(n1.nian==n2.nian&&n1.yue<n2.yue)||(n1.nian==n2.nian&&n1.yue==n2.yue&&n1.ri<n2.ri)) {
        return true;
    }else if(n1.nian==n2.nian&&n1.yue==n2.yue&&n1.ri==n2.ri){
        return false;
    }else{
        return false;
    }
}
int main() {
    long long n;
    cin>>n;
    for(int i=0; i<n; i++) {
        cin>>a[i].name>>a[i].nian>>a[i].yue>>a[i].ri;
    }
    sort(a,a+n,cmp);
    for(int i=0; i<n; i++) {
        cout<<a[i].name<<endl;
    }
    return 0;
}

by Eason20120229 @ 2024-08-28 21:10:01

@aguo_nuts (如果有两个同学生日相同,输入靠后的同学先输出) AC代码,求关注

#include <algorithm>
#include <iostream>
#include <string>
#define N 101

struct oi
{
    std::string name; //名字
    int y; //年
    int m; //月
    int d; //日
    int id; //编号
} arr[N];

bool cmp(const oi &one, const oi &two)
{
    if (one.y == two.y)
    {
        if (one.m == two.m)
        {
            if (one.d == two.d)
            {
                return one.id > two.id;
            }
            return one.d < two.d;
        }
        return one.m < two.m;
    }
    return one.y < two.y;
}

auto main() -> int
{
    int num;
    std::cin >> num;
    for (int i = 1; i <= num; i++)
    {
        std::cin >> arr[i].name >> arr[i].y >> arr[i].m >> arr[i].d;
        arr[i].id = i;
    }
    std::sort(arr + 1, arr + 1 + num, cmp);
    for (int i = 1; i <= num; i++)
    {
        std::cout << arr[i].name << std::endl;
    }
    return 0;
}

by aguo_nuts @ 2024-08-28 23:03:41

@Eason20120229 OKK谢谢好人,已关


|