PAT 1028 List Sorting

http://pat.zju.edu.cn/contests/pat-practise/1028

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <algorithm>
5 using namespace std;
6 struct Stu{
7 char ID[7];
8 char name[9];
9 int score;
10 }student[100002];
11 int meth;
12 bool cmp(struct Stu a,struct Stu b)
13 {
14 if(meth==1){
15 return strcmp(a.ID,b.ID)<0;
16 }else if(meth==2){
17 int re= strcmp(a.name,b.name);
18 if(re==0){
19 return strcmp(a.ID,b.ID);
20 }
21 return re<0;
22 }else{
23 if(a.score!=b.score){
24 return a.score<b.score;
25 }else{
26 return strcmp(a.ID,b.ID)<0;
27 }
28 }
29 }
30 int main()
31 {
32 int n;
33 scanf("%d%d",&n,&meth);
34 int i;
35 for(i=0;i<n;i++){
36 scanf("%s%s%d",&student[i].ID,&student[i].name,&student[i].score);
37 }
38 sort(student,student+n,cmp);
39 for(i=0;i<n;i++){
40 printf("%s %s %d\n",student[i].ID,student[i].name,student[i].score);
41 }
42 return 0;
43 }



原文地址:https://www.cnblogs.com/yangce/p/2415326.html