题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
#include <iostream> using namespace std; #include <vector> #include <map> #include <sstream> #include <algorithm> int main() { int nInputNum = 0; cin >> nInputNum; if (nInputNum != 0) { int nSortWay = 0; cin >> nSortWay; vector<string> vecInputSting;// 由name和成绩共同组成 map<int, vector<string>> mapInputSting;// 由name list和成绩共同组成 vector<string> vecInputName; vector<string> vecInputScore; while (nInputNum--) { string strInputName; cin >> strInputName; string strInputScore; cin >> strInputScore; int nInputScore = stoi(strInputScore); mapInputSting[nInputScore].push_back(strInputName); } vector<string> vecOutput; if (nSortWay == 1) { for (pair<int, vector<string>> pairVal : mapInputSting) { for (int i = 0; i < pairVal.second.size(); i++) { vecOutput.push_back(pairVal.second[i] + " " + to_string(pairVal.first)); } } for (int i = 0; i < vecOutput.size(); i++) { cout << vecOutput[i] << "\n"; } } else if (nSortWay == 0) { for (pair<int, vector<string>> pairVal : mapInputSting) { vector<string> vecTmp = pairVal.second; reverse(vecTmp.begin(), vecTmp.end()); for (int i = 0; i < vecTmp.size(); i++) { vecOutput.push_back(vecTmp[i] + " " + to_string(pairVal.first)); } } for (int i = vecOutput.size() - 1; i >= 0; i--) { cout << vecOutput[i] << "\n"; } } } } // 64 位输出请用 printf("%lld")