题解 | #成绩排序#
成绩排序
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存储分数和对应姓名。
查看45道真题和解析