题解|#成绩排序# C++ 利用map底层排序,最简洁代码

成绩排序

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

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

int main() {
    int n, f;
    cin >> n >> f;
    map<int, vector<string>> strmap;
    string name;
    int grade;
    for (int i = 0; i < n; i++) {
        cin >> name;
        cin >> grade;
        if (f == 1) {
            strmap[grade].push_back(name);
        } else {
            strmap[-grade].push_back(name);
        }
    }

    for (auto iter = strmap.begin(); iter != strmap.end(); iter++) {
        if (f == 1) {
            for (auto i : iter->second) {
                cout << i << " " << iter->first << endl;
            }
        } else {
            for (auto i : iter->second) {
                cout << i << " " << -iter->first << endl;
            }
        }
    }

    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务