成绩排序(巧妙利用vector)
#include <iostream> #include <vector> #include <string> using namespace std; int main() { int n; int way; string name; int grade; while(cin >> n >> way) { vector<string> b[101]; //0-100分的桶共101个 for(int i = 0; i < n; i++) { cin >> name >> grade; b[grade].push_back(name); } if(way == 0) { for(int i = 100 ; i >= 0; i--) { for(int j = 0; j < b[i].size(); j++) cout << b[i][j] << " " << i << endl; } } else { for(int i = 0 ; i < 101; i++) { for(int j = 0; j < b[i].size(); j++) cout << b[i][j] << " " << i << endl; } } } }