题目
代码
1 #include<iostream> 2 #include<algorithm> 3 #include<string.h> 4 using namespace std; 5 typedef struct stu{ 6 char cno[10]; 7 char name[10]; 8 int score; 9 }stu; 10 int n,c; 11 bool comp(stu a, stu b){ 12 if(c == 1) 13 //return a.cno < b.cno; 14 return strcmp(a.cno, b.cno) < 0; 15 else if(c == 2) { 16 if(strcmp(a.name, b.name) != 0) return strcmp(a.name, b.name) < 0; 17 else return strcmp(a.cno, b.cno) < 0; 18 } 19 else{ 20 if(a.score != b.score) return a.score < b.score; 21 else return strcmp(a.cno, b.cno) < 0; 22 } 23 } 24 int main(){ 25 stu s[100010]; 26 while(scanf("%d%d",&n,&c) != EOF){ 27 for(int i = 0;i < n;i++){ 28 scanf("%s%s%d",&s[i].cno,&s[i].name,&s[i].score); 29 } 30 sort(s,s+n,comp); 31 printf("Case: "); 32 for(int i = 0;i < n;i++){ 33 printf("%s %s %d ",s[i].cno,s[i].name,s[i].score); 34 } 35 puts(""); 36 } 37 return 0; 38 }
1. 字符串比较不能直接 小于等于大于号? 用strcmp
strcmp 与 小于等于大于的区别