求助90WA

P1093 [NOIP2007 普及组] 奖学金

zxm_ @ 2023-03-06 18:45:13

rt

#include<bits/stdc++.h> 
using namespace std;
struct student{
    int id;
    int yw,sx,yy;
    int sum;
    bool operator <(const student b) const{
        if(sum!=b.sum){
            return sum>b.sum;
        }
        else if(sum==b.sum){
            return yw>b.yw;
        }
        else{
            return id>b.id; 
        }
    } 
}stu[10893]; 

int main(){

    int n;
    cin>>n; 
    for(int i=1;i<=n;i++){
        cin>>stu[i].yw>>stu[i].sx>>stu[i].yy; 
        stu[i].id=i;
        stu[i].sum=stu[i].yw+stu[i].yy+stu[i].sx;
    }
    sort(stu,stu+n+1);
    for(int i=0;i<=4;i++){
        cout<<stu[i].id<<" "<<stu[i].sum<<endl;
    }
}

什么?为什么用operator重载?老师让这么写一遍


by jiangjiangQwQ @ 2023-03-06 19:04:28

#include<bits/stdc++.h>

using namespace std;
struct student {
    int id;
    int yw, sx, yy;
    int sum;
    bool operator < (const student b) const {
        if (sum != b.sum) {
            return sum > b.sum;
        } else if (yw != b.yw) {
            return yw > b.yw;
        }
        return id < b.id;
    }
}
stu[10893];

int main() {

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> stu[i].yw >> stu[i].sx >> stu[i].yy;
        stu[i].id = i;
        stu[i].sum = stu[i].yw + stu[i].yy + stu[i].sx;
    }
    sort(stu + 1, stu + n + 1);
    for (int i = 1; i <= 5; i++) {
        cout << stu[i].id << " " << stu[i].sum << endl;
    }
}

AC代码给你,你看一下 @zxm_


by happy_zero @ 2023-03-06 19:05:41

bool operator <(const student b) const{
        if(sum!=b.sum){
            return sum>b.sum;
        }
        else if(sum==b.sum){//1.
            return yw>b.yw;
        }
        else{
            return id<b.id; //2.
        }
    } 

@zxm_


by OIerBoy @ 2023-03-06 19:05:44

@zxm_ 11行

else if(yw!=b.yw)

15行

return id<b.id; 

by zxm_ @ 2023-03-06 19:46:42

@happy_zero @Dream_zaa @jiangshuhong

谢谢指导(日常脑抽)


|