2WA 8RE

P1093 [NOIP2007 普及组] 奖学金

I_AK_IOI_EVERY_DAY @ 2023-12-06 09:46:29

#include<bits/stdc++.h>
using namespace std;
int n;
struct stu{
    int Chinese,math,English,sum,name;
}a[301];
bool cmp(stu a,stu b){
    if(a.sum>b.sum){
        return a.sum>b.sum; 
    }
    else if(a.sum==b.sum&&a.English>b.English){
        return a.sum>b.sum;
    }
    else if(a.sum==b.sum&&a.English==b.English&&a.name<b.name){
        return a.sum>b.sum;
    }
    else{
         return a.sum<b.sum;
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].Chinese>>a[i].math>>a[i].English;
        a[i].sum=a[i].Chinese+a[i].math+a[i].English;
        a[i].name=i; 
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=5;i++){
        cout<<a[i].name<<" "<<a[i].sum;
    }
    return 0;
}

by LeoYangcz @ 2023-12-23 17:09:11

1. cmp内的if是!=

求关
#include <bits/stdc++.h>
using namespace std;
struct student{
    int x;
    double u;
    double s;
    double y;
    double sum;
}a[305]; 
bool cmp(student a,student b){
    if(a.sum!=b.sum){
        return a.sum>b.sum;
    }
    if(a.u!=b.u){
        return a.u>b.u;
    }
    return a.x<b.x;
} 
int main() {
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i].u;
        cin>>a[i].s;
        cin>>a[i].y;
        a[i].x=i+1;
        a[i].sum=a[i].u+a[i].s+a[i].y;
    } 
    sort(a,a+n,cmp);
    for(int i=0;i<5;i++){
        cout<<a[i].x<<" "<<a[i].sum<<endl;
    }
    return 0;
}

|