题解 | #成绩排序#
成绩排序
http://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
主要是想到map<int, vector<string>>
#include<iostream> #include<vector> #include<map> #include<string> using namespace std; int main(){ int n; while(cin >> n){ int flag; cin >> flag; string str; int score; map<int, vector<string>> m; for(int i = 0; i < n; ++i){ cin >> str >> score; m[score].push_back(str); } if(flag == 1){ for(auto it = m.begin(); it != m.end(); ++it){ for(int i = 0; i < it->second.size(); ++i){ cout << it->second[i] << " " << it->first << endl; } } }else{ for(auto it = m.rbegin(); it != m.rend(); ++it){ for(int i = 0; i < it->second.size(); ++i){ cout << it->second[i] << " " << it->first << endl; } } } } return 0; }