题解 | #成绩排序#

成绩排序

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;
}

全部评论

相关推荐

06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
牛客266927136号:为啥实习经历写这么少,项目经历反而大写特写,最重要的还是实习经历吧,写具体点,什么场景下做了什么事,解决了什么问题,优化了什么场景,性能提升了多少多少
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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