Posted on 2011-09-05 22:20
acpeng 阅读(442)
评论(0) 编辑 收藏 引用 所属分类:
ACM程序
http://acm.hdu.edu.cn/showproblem.php?pid=1862结构体排序,C++的algorithm,写三个cmp即可
不过以这个数据的话,何必
TimeLimit=5000ms~~
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct infor
{
char stu_num[7];
char name[9];
int scor;
}stu;
stu data[100002];
int cmp1(stu a,stu b)
{
if(strcmp(a.stu_num,b.stu_num)<0)return 1;
return 0;
}
int cmp2(stu a,stu b)
{
if(strcmp(a.name,b.name)<0)return 1;
else if(strcmp(a.name,b.name)==0)
{
if(strcmp(a.stu_num,b.stu_num)<0)return 1;
}
return 0;
}
int cmp3(stu a,stu b)
{
if(a.scor<b.scor)return 1;
else if(a.scor==b.scor)
{
if(strcmp(a.stu_num,b.stu_num)<0)return 1;
}
return 0;
}
int main()
{
int n,c,i,test=1;
while(scanf("%d%d",&n,&c),n)
{
for(i=0;i<n;i++)
scanf("%s%s%d",data[i].stu_num,data[i].name,&data[i].scor);
if(c==1)sort(data,data+n,cmp1);
else if(c==2)sort(data,data+n,cmp2);
else sort(data,data+n,cmp3);
printf("Case %d:\n",test++);
for(i=0;i<n;i++)
printf("%s %s %d\n",data[i].stu_num,data[i].name,data[i].scor);
}
return 0;
}