题解 | #成绩排序#

成绩排序

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

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

int main() {
    int n;
    while (cin >> n) {
        int low_to_high;
        cin>>low_to_high;
        vector<pair<int, string>> content(n);
        for(int i=0;i<n;i++){
            int score;
            string name;
            cin>>name>>score;
            content[i] = {score, name};
        }// 不能使用greater<>和less<> first相等的时候会比较second
        if(low_to_high) stable_sort(content.begin(), content.end(), [](
            const pair<int, string>& a, const pair<int, string>& b){
                return a.first<b.first;
            });
        else{
            stable_sort(content.begin(), content.end(), [](
                        const pair<int, string>& a, const pair<int, string>& b){
                return a.first>b.first;
            });
        }
        for(int i=0;i<n;i++){
            cout<<content[i].second<<' '<<content[i].first<<endl;
        }
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
08-20 19:41
那一天的Java_J...:简历完全流水账,学生思维很严重,还有很大的优化空间,可以多看看牛客的简历。
点赞 评论 收藏
分享
否极泰来来来来:解约赔多少
点赞 评论 收藏
分享
看到好多帖子双9都kuku挂,双9都挂,那我还投啥啊
_追梦旅人_:同学考虑我司不,我们正在秋招,可在我主页看岗位,感兴趣可直接投递~
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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