求助

P1104 生日

AKkkk_TIX @ 2024-08-07 17:03:27

用了运算符重载,注意不要开O2,要不然全RE

#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;
    }
    bool operator < ( stu x ) const {
        return ( 
                 year < x.year 
              || year == x.year && month < x.month 
              || year == x.year && month == x.month && day < x.day 
              || year == x.year && month == x.month && day == x.day 
                                                    && num > x.num
              );
    }
} 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] < a[j - 1] ){
                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 Yue_Hao @ 2024-08-09 12:28:29

不是我说,这题的样例很水的,根本不用这么复杂,简单的sort+cmp就AC了,不信你拿去试试,并且你这样写自我感觉很容易错,题目说了生日相同的id后的排前, 话不多说,上AC代码:

//头文件和结构体不用说吧
//核心
bool cmp(Node x, Node y){
    if(x.y != y.y) return x.y < y.y;
    else if(x.m != y.m) return x.m < y.m;
    else if(x.d != y.d) return x.d < y.d;
    else return x.name > y.name;
}
int main(){
    long long n;
    //输入不用说吧
    sort(a, a+n, cmp);
    //输出不用说吧
    return 0;
}

by Yue_Hao @ 2024-08-09 12:29:35

水样例的就这么写吧(doge)


|