题解 | #成绩排序#

成绩排序

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

#include <functional>
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;

int main() {
    int n, mode;
    cin >> n >> mode;
    if (mode == 0) {
        map<int, vector<string>, greater<>> map;
        for (int i = 0; i < n; i++) {
            string name;
            int score;
            cin >> name >> score;
            map[score].push_back(name);
        }
        for (auto it = map.begin(); it != map.end(); it++) {
            for (int i = 0; i < it->second.size(); i++) {
                cout << it->second[i] << ' ' << it->first << endl;
            }
        }
    } else {
        map<int, vector<string>, less<>> map;
        for (int i = 0; i < n; i++) {
            string name;
            int score;
            cin >> name >> score;
            map[score].push_back(name);
        }
        for (auto it = map.begin(); it != map.end(); it++) {
            for (int i = 0; i < it->second.size(); i++) {
                cout << it->second[i] << ' ' << it->first << endl;
            }
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

使用map存储分数和对应姓名。

全部评论

相关推荐

04-17 10:16
门头沟学院 Java
不河狸啊:为什么我的是已送达,连已读都没有
点赞 评论 收藏
分享
可以不说话:笔试a了3道半,今天说是挂了😭😭
投递汇丰科技等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务