寒假每日一题day9 AcWing 429. 奖学金(结构体排序)

AcWing 429. 奖学金

题意:

按照总分先排序;
若相等,按照语文排序。
若相等, 按照学号升序排序。

思路:

结构体sort一下就好了。
注意不等时,才到下个if

AC

# include <bits/stdc++.h>

using namespace std;
struct  stu{
    
    
    int m, c, e;
    int id, tot;
    bool operator < (const stu x)const{
    
    
        if(tot!=x.tot)return tot>x.tot;
        else if(c!=x.c)return c>x.c;
        else return id<x.id;
    }
} p[340];
int main(){
    
    
    int n;
    cin>>n;
    for(int i = 1; i <= n; i ++ ){
    
    
        int c, m, e;
        cin>>c>>m>>e;
        p[i]={
    
    m,c,e,i,c+m+e};
      //  cout<<p[i].tot<<endl;
    }
    sort(p+1,p+1+n);
    for(int i = 1; i <= 5; i ++ )cout<<p[i].id<<' '<<p[i].tot<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45377553/article/details/112737656