题目:http://acm.hdu.edu.cn/showproblem.php?pid=1862
这道题容易TLE,我提交了好多次,最后发现原来是选择判断是用了if而没有用switch,
最后上网找了一下,发现在多次需要判断的情况下,if所用时间要大于switch,
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
int number;
char name[10];
int score;
}student;
student stu[100100];
int cmp3(const void *a,const void *b)
{
student *aa=(student *)a;
student *bb=(student *)b;
if(aa->score!=bb->score)
return aa->score - bb->score;
else
return aa->number-bb->number;
}
int cmp2(const void *a,const void *b)
{
student *aa=(student *)a;
student *bb=(student *)b;
if(strcmp(aa->name,bb->name))
return strcmp(aa->name,bb->name);
else
return aa->number-bb->number;
}
int cmp1(const void *a,const void *b)
{
student *aa=(student *)a;
student *bb=(student *)b;
return aa->number-bb->number;
}
int main()
{
int n,c,t=1;
while(scanf("%d%d",&n,&c))
{
if(n==0&&c==0)
break;
for(int i=0;i<n;i++)
scanf("%d%s%d",&stu[i].number,stu[i].name,&stu[i].score);
switch(c)
{
case 1:
qsort(stu, n, sizeof(stu[0]), cmp1);
break;
case 2:
qsort(stu, n, sizeof(stu[0]), cmp2);
break;
case 3:
qsort(stu, n, sizeof(stu[0]), cmp3);
break;
}
printf("Case %d:\n",t++);
for(int i=0;i<n;i++)
printf("d %s %d\n",stu[i].number,stu[i].name,stu[i].score);
}
return 0;
}