16分求助

P1104 生日

Real_Luka_Modric @ 2023-09-09 20:38:59

#include <bits/stdc++.h>
using namespace std;
struct stu{
    string name;
    int nian,yue,ri,bian;
}a[105];
bool cmp(stu a,stu b){
    if(a.nian==b.nian){
        if(a.yue==b.yue){
            if(a.ri==a.ri){
                return a.bian>b.bian;
            }else{
                return a.ri>b.ri;
            }
        }
        else{
            return a.yue>b.yue;
        }
    }
    else return a.nian<b.nian;
}
int main(){
    int n,m;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].name>>a[i].nian>>a[i].yue>>a[i].ri;
        a[i].bian=i;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++){
        cout<<a[i].name<<endl;
    }
    return 0;
}

by OIerWu_829 @ 2023-09-09 20:44:56

#include <bits/stdc++.h>
using namespace std;

struct node
{
    int y, m, d, pm;
    string name;
}a[110];

bool cmp(node x, node y)
{
    if (x.y != y.y) return x.y < y.y;
    if (x.m != y.m) return x.m < y.m;
    if (x.d != y.d) return x.d < y.d;
    return x.pm > y.pm;
}

int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].name >> a[i].y >> a[i].m >> a[i].d;
        a[i].pm = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        cout << a[i].name << endl;
    }
    return 0;
}

by OIerWu_829 @ 2023-09-09 20:45:36

My AC Code,结构体排序,应该能看懂吧


by sugawara @ 2023-09-09 20:48:02

从你的代码来看的话问题确实不大,但是从测评记录上来看的话大概是排序的问题,这里我觉得是你的cmp的问题,可以把if分开写,因为这是一个函数return之后就没了,所以if一个return一个


by MJLY @ 2023-09-09 21:34:01

首先是逻辑问题,应该是年份,月份小的的年龄大,也就是把结构体判断else中大于改为小于;其次是第十行应该是a.ri==b.ri


by ai_inan @ 2023-09-10 21:59:22

@Real_Luka_Modric AC代码:

#include <bits/stdc++.h>
using namespace std;

struct node
{
    int y, m, d, pm;
    string name;
}a[110];

bool cmp(node x, node y)
{
    if (x.y != y.y) return x.y < y.y;
    if (x.m != y.m) return x.m < y.m;
    if (x.d != y.d) return x.d < y.d;
    return x.pm > y.pm;
}

int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].name >> a[i].y >> a[i].m >> a[i].d;
        a[i].pm = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        cout << a[i].name << endl;
    }
    return 0;
}

|