题解 | #成绩排序#

成绩排序

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")

全部评论

相关推荐

测试糕手手:社会第一课,随便吹牛逼,直接说四个月,别老实。老实人只会被欺负
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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