题解 | #成绩排序#
成绩排序
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")