60求调

P1093 [NOIP2007 普及组] 奖学金

yzx2013 @ 2024-10-25 19:59:54

#include<bits/stdc++.h>
using namespace std;
int n;
struct s{
    int yw,sx,yy,zf,xh;
}a[301];
bool cmp(s x,s y){
    if(x.zf==y.zf){
        return x.xh>y.xh;
    }
    return x.zf>y.zf;
}
int main(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].yw>>a[i].sx>>a[i].yy;
        a[i].zf=a[i].yw+a[i].sx+a[i].yy;
        a[i].xh=i;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=5;i++){
        cout<<a[i].xh<<" "<<a[i].zf<<"\n";
    }
    return 0;
}

by Ff472130 @ 2024-10-25 20:13:36

先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学排在前面

代码

#include<bits/stdc++.h>
using namespace std;
int n;
struct s{
    int yw,sx,yy,zf,xh;
}a[301];
bool cmp(s x,s y){
    if (x.zf!=y.zf) return x.zf>y.zf;
    if (x.yw!=y.yw) return x.yw>y.yw;
    return x.xh<y.xh;
}
int main(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].yw>>a[i].sx>>a[i].yy;
        a[i].zf=a[i].yw+a[i].sx+a[i].yy;
        a[i].xh=i;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=5;i++){
        cout<<a[i].xh<<" "<<a[i].zf<<"\n";
    }
    return 0;
}

求关


|