//ac了
//上面说的不对,ifelse是可以和switch互换的,我用switch错了的缘故是case '1',其实应该是case '1',因为c是整型
//switch是不能乱用的啊!就老老实实也欧诺个if else .这一题一开始用switch就错了!
#include<cstdio>
#include<cstring>
#include<algorithm>
//#define LOCAL
using namespace std;
struct Student{
int id;//6 digits
char name[10];
int grade;//成绩在0~100
}stu[100010];
bool cmp1(Student a,Student b){
return a.id<b.id;
}
bool cmp2(Student a,Student b){//按照姓名大小
int s=strcmp(a.name,b.name);
if(s!=0) return s<0;
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(){
#ifdef LOCAL
freopen("A1028data.in","r",stdin);
freopen("A1028data.out","w",stdout);
#endif
int n,c;
scanf("%d %d",&n,&c);
for(int i=0;i<n;i++){
scanf("%d %s %d",&stu[i].id,stu[i].name,&stu[i].grade);
}
switch(c){
case 1:sort(stu,stu+n,cmp1);break;
case 2:sort(stu,stu+n,cmp2);break;
case 3:sort(stu,stu+n,cmp3);break;
/*if(c==1)sort(stu,stu+n,cmp1);//按id递增排序
else if(c==2)sort(stu,stu+n,cmp2);//按姓名非降序排序,id升序
else sort(stu,stu+n,cmp3);//成绩不降序排序,id升序
这样也可以的!*/
}
for(int i=0;i<n;i++){
printf("%06d %s %d\n",stu[i].id,stu[i].name,stu[i].grade);
}
return 0;
}
PAT A1028 List Sorting
最新推荐文章于 2022-01-13 16:09:10 发布