题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

struct student{
    string name;
    int score;
    student(string n,int s):name(n),score(s){}
};

bool compare0(student s1,student s2){
    return s1.score>s2.score;
}
bool compare1(student s1,student s2){
    return s1.score<s2.score;
}

int main(){
    vector<student> list;
    string name;
    int n,flag,s;
    while (cin>>n>>flag){
        for (int i = 0; i < n; ++i) {
            cin>>name>>s;
            list.push_back(student(name,s));
        }
        if (flag){
            stable_sort(list.begin(),list.end(), compare1);
        } else{
            stable_sort(list.begin(),list.end(), compare0);
        }
        for (int i = 0; i < n; ++i) {
            cout<<list[i].name<<" "<<list[i].score<<endl;
        }
    }

    return 0;
}

全部评论

相关推荐

05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 11:15
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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