32分求助

P1104 生日

AKkkk_TIX @ 2024-08-23 17:43:57

#include <bits/stdc++.h>
using namespace std;
struct stu {
    string name;
    int year, month, day, num;
    friend istream & operator >> ( istream &in, stu &x ) {
        cin >> x.name >> x.year >> x.month >> x.day;
        return cin;
    }
} a[100];
int n, i, j;
int main() {
    cin >> n;
    for (i = 0; i < n; i++){
        cin >> a[i];
        a[i].num = i + 1;
    }
    for(i = 0;i < n - 1;i++) {
        bool b = true;                                                                  
        for(j = n - 1;j >= i + 1;j--)
            if ( a[j].year < a[j - 1].year 
              || a[j].year == a[j - 1].year && a[j].month < a[j - 1].month 
              || a[j].year == a[j - 1].year && a[j].month == a[j - 1].month && a[j].day < a[j - 1].day 
              || a[j].year == a[j - 1].year && a[j].month == a[j - 1].month && a[j].day == a[j - 1].day 
                                                    && a[j].num > a[j - 1].num ){
                swap ( a[i], a[j] );
                b = false;
            }
        if( b ) break;
    }

    for (i = 0; i < n; i++){
        cout << a[i].name;
        if(i != n - 1)
            cout << "\n";
    }
    return 0;
}

记录详情


by SmileString @ 2024-08-23 17:59:04

@a_programmer

#include<bits/stdc++.h>
using namespace std;
int n;
struct tx{
    string name;
    int year,month,day,No;
};
int cmp(tx a,tx b)
{
    if(a.year!=b.year)return a.year>b.year;
    if(a.month!=b.month)return a.month>b.month;
    if(a.day!=b.day)return a.day>b.day;
    return a.No<b.No;
}
int main()
{
    cin>>n;
    tx s[102];
    for(int i=0;i<n;i++)
    {
        cin>>s[i].name>>s[i].year>>s[i].month>>s[i].day;
        s[i].No=i;
    }
    stable_sort(s,s+n,cmp);
    for(int i=n-1;i>=0;i--)
        cout<<s[i].name<<endl;
    return 0;
}

by gaoyunxuan0917 @ 2024-08-23 20:07:57

@a_programmer

#include<bits/stdc++.h>
using namespace std;
int n;
struct f{
    string name;
    int year;
    int mon;
    int day;
    int id;
}r[105];
bool cmp(f a,f b){
    if(a.year != b.year) {
        return a.year < b.year;
    } else {
        if(a.mon != b.mon) {
            return a.mon < b.mon;
        } else {
            if(a.day == b.day && a.mon == b.mon) {
                return a.id > b.id;
            } else {
                if(a.day != b.day && a.mon == b.mon) {
                    return a.day < b.day;
                }
            }
        }
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> r[i].name >> r[i].year >> r[i].mon >> r[i].day;
        r[i].id = i;
    }
    sort(r + 1, r + n + 1, cmp);
    for (int i = 1; i <= n; i++) {
        cout << r[i].name << endl;
    }
    return 0;
}

记录详情


|