5/6轮着爆WA

P1104 生日

YearnstudioYangyi @ 2024-12-05 20:21:20

我代码轮着爆5、6

5号WA

#include<bits/stdc++.h>
using namespace std;
class people{
public:
    string name;
    int y,m,d;
};
bool operator<(people a,people b){
    if(a.y != b.y){
        return a.y < b.y;
    }else if(a.m != b.m){
        return a.m < b.m;
    }else if(a.d != b.d){
        return a.d < b.d;
    }
    return true;
}
int main(){
    int n;
    cin >> n;
    vector<people> num;
    for(int t = 0;t < n;t++){
        people temp;
        cin >> temp.name >> temp.y >> temp.m >> temp.d;
        num.push_back(temp);
    }
    sort(num.begin(),num.end());
    for(int t = 0;t < n;t++){
        cout << num[t].name << endl;
    }
    return 0;
}

6号测试点WA

#include<bits/stdc++.h>
using namespace std;
class people{
public:
    string name;
    int y,m,d;
};
bool operator<(people a,people b){
    if(a.y != b.y){
        return a.y < b.y;
    }else if(a.m != b.m){
        return a.m < b.m;
    }else if(a.d != b.d){
        return a.d < b.d;
    }
    return false;
}
int main(){
    int n;
    cin >> n;
    vector<people> num;
    for(int t = 0;t < n;t++){
        people temp;
        cin >> temp.name >> temp.y >> temp.m >> temp.d;
        num.push_back(temp);
    }
    sort(num.begin(),num.end());
    for(int t = 0;t < n;t++){
        cout << num[t].name << endl;
    }
    return 0;
}

不同之处:21、58行返回

求助但是不要给题解,指出错误即可,谢谢


by _jimmywang_ @ 2024-12-05 20:28:36

如果有两个同学生日相同,输入靠后的同学先输出


by YearnstudioYangyi @ 2024-12-08 17:41:19

@jimmywang 我考虑到了,<重载的return false和true轮着上,但是都在爆


by _jimmywang_ @ 2024-12-09 00:04:35

咱就是说,快速排序这个函数本身并不保证在 cmp 比不出来的时候维持原序。换句话说,过了一遍快排以后,生日相同的人的顺序已经乱了。这无所谓 cmp 函数的返回值,是快排函数本身导致的。


by jlm888 @ 2025-01-08 18:53:29

666


|