题解 | #成绩排序#

成绩排序

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

greater<int>好久不用了,记录一下

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

int main() {
    int n, flag;
    while (cin >> n >> flag) {
        multimap<int, string> map1; // 使用 std::map<int,string> 作为底层容器
        string str;
        int number;
        while (n--) {
            cin >> str >> number;
            map1.insert({number, str});
        }

        if (flag == 1) {
            for (auto& ch : map1) {
                cout << ch.second << " " << ch.first << endl;
            }
        } else if (flag == 0) {
            // for(auto i = map1.size()-1;i>=0;i--){
            //     cout<<map1[i].second<<" "<<map1[i].first<<endl;
            //
            //std::map 是一个关联容器,其内部的元素是按键(key)进行排序的,默认情况下按升序排列。因此,使用 for(auto i = map1.size()-1;i>=0;i--) 进行遍历是错误的,因为 std::map 不支持使用下标访问元素。
            multimap<int,string,greater<int>>map2(map1.begin(),map1.end());
            for (auto& ch : map2) {
                cout << ch.second << " " << ch.first << endl;
            }
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-05 15:27
点赞 评论 收藏
分享
04-13 18:10
门头沟学院 Java
想熬夜的小飞象在秋招:被腾讯挂了后爸妈以为我失联了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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