题解 | #成绩排序#

成绩排序

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

#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
#include <utility>
#include <vector>
using namespace std;



int main() {
    int n;
    cin >> n;
    int flag;
    cin >> flag;
    vector<pair<string, pair<int, int>>> data(n);
    for (int i = 0; i < n; ++i) {
        string name;
        int score;
        cin >> name >> score;
        data[i] = {name, {score, i}};
    }
    if (flag) {
        sort(begin(data), end(data), [](pair<string, pair<int, int>>& a, pair<string, pair<int, int>>& b){return a.second.first != b.second.first ? a.second.first < b.second.first : a.second.second < b.second.second;});
    } else {
        sort(begin(data), end(data), [](pair<string, pair<int, int>>& a, pair<string, pair<int, int>>& b){return a.second.first != b.second.first ? a.second.first > b.second.first : a.second.second < b.second.second;});
    }
    for (const auto &[name, p] : data) {
        cout << name << " " << p.first << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

迷茫的大四🐶:都收获五个了,兄弟那还说啥,不用改了,去玩吧
点赞 评论 收藏
分享
27届毕业,最近想找一段大厂实习,感觉简历有些问题,好多都不给面,求大佬们指点,最近好焦虑
重生之我学Java干...:我从后端的角度分析一下你的第一个项目,我感觉亮点不是很突出。因为我是因为组内有需求,临时上手学react干活。我用到的技术基本就cover你那个智慧园区管理平台的很多亮点了。那作为比较专业的前端,你上述的内容是不是有点单薄呢。感觉还得包装
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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