1028 List Sorting
代码如下:
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 #include<iomanip> 5 using namespace std; 6 struct info{ 7 int id; 8 string name; 9 int score; 10 }; 11 bool cmp1(info i1,info i2){ 12 return i1.id < i2.id; 13 } 14 bool cmp2(info i1,info i2){ 15 if(i1.name < i2.name) 16 return true; 17 else if(i1.name == i2.name && i1.id < i2.id) return true; 18 return false; 19 } 20 bool cmp3(info i1,info i2){ 21 if(i1.score < i2.score) 22 return true; 23 else if(i1.score == i2.score && i1.id < i2.id) return true; 24 return false; 25 } 26 27 int main(){ 28 vector<info> vec; 29 info in; 30 int n,m; 31 cin >> n >> m; 32 for(int i = 0; i < n; i++){ 33 cin >> in.id >> in.name >> in.score; 34 vec.push_back(in); 35 } 36 if(m == 1){ 37 sort(vec.begin(),vec.end(),cmp1); 38 } 39 else if(m == 2){ 40 sort(vec.begin(),vec.end(),cmp2); 41 } 42 else{ 43 sort(vec.begin(),vec.end(),cmp3); 44 } 45 for(int i = 0; i < n; i++){ 46 cout << setw(6) << setfill('0') << vec[i].id << " "; 47 cout << vec[i].name << " "; 48 cout << vec[i].score << endl; 49 } 50 return 0; 51 }