标签:
1 #include <vector> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 7 struct MyStruct 8 { 9 char ID[7]; 10 char name[9]; 11 int grade; 12 }; 13 14 int cmp1(MyStruct a,MyStruct b) 15 { 16 return (strcmp(a.ID,b.ID)<0); 17 } 18 19 int cmp2(MyStruct a,MyStruct b) 20 { 21 if(strcmp(a.name,b.name)==0) return (strcmp(a.ID,b.ID)<0); 22 else return strcmp(a.name,b.name)<0; 23 } 24 25 26 int cmp3(MyStruct a,MyStruct b) 27 { 28 if(a.grade==b.grade) return (strcmp(a.ID,b.ID)<0); 29 else return a.grade<b.grade; 30 } 31 32 33 int main() 34 { 35 int n,c,i; 36 while(scanf("%d %d",&n,&c)!=EOF) 37 { 38 vector <MyStruct> vv; 39 for(i=0;i<n;i++) 40 { 41 MyStruct tem; 42 getchar(); 43 scanf("%s %s %d",tem.ID,tem.name,&tem.grade); 44 vv.push_back(tem); 45 } 46 if(c==1) 47 sort(vv.begin(),vv.end(),cmp1); 48 if(c==2) 49 sort(vv.begin(),vv.end(),cmp2); 50 if(c==3) 51 sort(vv.begin(),vv.end(),cmp3); 52 53 for(i=0;i<n;i++) 54 printf("%s %s %d\n",vv[i].ID,vv[i].name,vv[i].grade); 55 56 } 57 return 0; 58 }
标签:
原文地址:http://www.cnblogs.com/xiaoyesoso/p/4265168.html