[PAT] Class 1028 List Sorting (25 points)

Meaning of the questions:

Enter a positive integer N (<= 100000) and C (C belonging to {1,2,3}), followed by N input rows, each row comprising six school student number (string input with diet, because there may be a leading zero ), the names and accomplishments (a positive integer). Outputting ordering information, sorted according to the determined C.

Code:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
typedef struct student{
string id,name;
int grade;
};
student s[100007];
bool cmp(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;
return a.id<b.id;
}
bool cmp3(student a,student b){
if(a.grade!=b.grade)
return a.grade<b.grade;
return a.id<b.id;
}
int main(){
int n,c;
cin>>n>>c;
for(int i=1;i<=n;++i)
cin>>s[i].id>>s[i].name>>s[i].grade;
if(c==1)
sort(s+1,s+1+n,cmp);
else if(c==2)
sort(s+1,s+1+n,cmp2);
else if(c==3)
sort(s+1,s+1+n,cmp3);
for(int i=1;i<=n;++i){
cout<<s[i].id<<" "<<s[i].name<<" "<<s[i].grade;
cout<<((i==n)?"":"\n");
}
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11481964.html