题解 | #EXCEL排序#
EXCEL排序
https://www.nowcoder.com/practice/bf3ec474bb7d410dbb9d5bbcd07a93e5
#include <iostream> #include <algorithm> #include <string> using namespace std; struct Student{ string id; string name; int grade; }; Student student[100000+1]; bool cmp1(Student a,Student b){ return a.id<b.id; } bool cmp2(Student a,Student b){ if (a.name!=b.name){//名字不相等按名字非降序输出 return a.name<b.name; } else{//名字相等即按学号 return a.id<b.id; } } bool cmp3(Student a,Student b){ if (a.grade!=b.grade){//成绩不相等按名字非降序输出 return a.grade<b.grade; } else{//成绩相等即按学号 return a.id<b.id; } } int main(){ int N,Case; while (cin>>N>>Case){ if (N==0) break; for (int i = 0; i < N; ++i) { cin>>student[i].id>>student[i].name>>student[i].grade; } if (Case==1){ sort(student,student+N, cmp1); } else if (Case==2){ sort(student,student+N, cmp2); } else{ sort(student,student+N, cmp3); } cout<<"Case:"<<endl; for (int i = 0; i < N; ++i) { cout<<student[i].id<<" "<<student[i].name<<" "<<student[i].grade<<endl; } } }