84求助,十万火急!!!

P1104 生日

Sprunki_Rabbit @ 2024-07-31 13:39:34

帮助有奖

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

struct stu{
    string name;
    int year,month,day;
}s[1001];

int cmp(stu x,stu y)
{
    /*
    if(a.year < b.year)
        a.year = b.year;
    if(a.month < b.month)
        a.month = b.month;
    if(a.day < b.day)
        a.day = b.day;
    */
    return x.year<y.year||x.year==y.year&&x.month<y.month||x.year==y.year&&x.month==y.month&&x.day<y.day||x.year==y.year&&x.month==y.month&&x.day==y.day;
}

int main()
{
    int n;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> s[i].name >> s[i].year >> s[i].month >> s[i].day;
    }
    sort(s+1,s+1+n,cmp);    
    for(int i=1;i<=n;i++)
        cout << s[i].name << endl;

    return 0;
}

by zcs_ @ 2024-07-31 13:42:37

@Katie_Rabbit 生日相同的情况排id


by Sprunki_Rabbit @ 2024-07-31 13:43:55

@zcs_accept 好的谢谢大佬


by lzdqwq @ 2024-07-31 13:44:41

#include<bits/stdc++.h>
using namespace std;
int n;
struct node{ 
    string name;
    int year;
    int month; 
    int day; 
    int num;
}a[1005];
bool cmp(node x,node y){
    if(x.year!=y.year)return x.year<y.year; 
    if(x.month!=y.month)return x.month<y.month;
    if(x.day!=y.day)return x.day<y.day;
    return x.num>y.num;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].name>>a[i].year>>a[i].month>>a[i].day;
        a[i].num=i;
    }
    sort(a+1,a+1+n,cmp); 
    for(int i=1;i<=n;i++)cout<<a[i].name<<endl; 
    return 0;
}

by Sprunki_Rabbit @ 2024-07-31 13:46:21

@lzdqwq 哇AC代码耶谢啦谢啦


by b2314huangruizhong @ 2024-07-31 13:46:24

@Katie_Rabbit

给你改了一下

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

struct stu{
    string name;
    int year,month,day,rk;
}s[1001];

bool cmp(stu x,stu y)//cmp函数最好用bool 返回
{
    if(x.year == y.year){
        if(x.month == y.month){
            if(x.day == y.day)return x.rk>y.rk;
            return x.day<y.day;
        }
        return x.month<y.month; 
    } 
    return x.year<y.year;
}

int main()
{
    int n;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> s[i].name >> s[i].year >> s[i].month >> s[i].day;
        s[i].rk=i;
    }
    sort(s+1,s+1+n,cmp);    
    for(int i=1;i<=n;i++)
        cout << s[i].name << endl;

    return 0;
}

by Sprunki_Rabbit @ 2024-07-31 13:46:58

谢谢帮忙,奖励关注☺️


by Sprunki_Rabbit @ 2024-07-31 13:48:53

@b2314huangruizhong 哇哇谢谢


by lijunxian_0818_2 @ 2024-10-15 12:51:04

@Katie_Rabbit

你这代码让我有一种我刚学C++时的感觉。


|