题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1

#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;

struct Student{
    string name;
    int score;
};

bool Compare_inc(Student a,Student b){
        return a.score<b.score;
}

bool Compare_dec(Student a,Student b){
        return a.score>b.score;
}

int main() {
    int n, mode;
    while (cin >> n >> mode) { // 注意 while 处理多个 case
        vector<Student> arr(n);
        for(int i=0;i<n;i++){
            cin>>arr[i].name>>arr[i].score;
        }
        if(mode)
            stable_sort(arr.begin(),arr.end(),Compare_inc);
        else
            stable_sort(arr.begin(),arr.end(),Compare_dec);
        for(auto item:arr){
            cout<<item.name<<" "<<item.score<<endl;
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

求问!考研下岸,打算参加春招,我这个bg能进啥厂,或者需要搞点深度项目再投吗
Java抽象带篮子_...:直接海投,可以看看我的考研失利速成冲春招贴,里面详细写了简历怎么写,学哪些项目可以速成
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务