[恢]hdu 1862

2011-12-27 13:44:49

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1862

题意:中文,排序。

代码:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>


typedef struct student{
char num[7] ;
char name[9] ;
int grade ;
}student ;


student stu[100010] ;


int cmp1(const void *a, const void *b)
{
student *p = (student*)a, *q = (student*)b ;
return strcmp(p->num, q->num) ;
}


int cmp2(const void *a, const void *b)
{
int rtn ;
student *p = (student*)a, *q = (student*)b ;
rtn = strcmp(p->name, q->name) ;
if (rtn == 0) return strcmp(p->num, q->num) ;
return rtn ;
}


int cmp3(const void *a, const void *b)
{
int rtn ;
student *p = (student*)a, *q = (student*)b ;
if (p->grade != q->grade) return p->grade - q->grade ;
return strcmp(p->num, q->num) ;
}


int main ()
{
int i, n, c, nCase = 1 ;
while (~scanf ("%d %d%*c", &n, &c) && (n||c))
{
for (i = 0 ; i < n ; i++)
scanf ("%s %s %d%*c", stu[i].num, stu[i].name, &stu[i].grade) ;
if (c == 1)
qsort (stu, n, sizeof (student), cmp1) ;
if (c == 2)
qsort (stu, n, sizeof (student), cmp2) ;
if (c == 3)
qsort (stu, n, sizeof (student), cmp3) ;
printf ("Case %d:\n", nCase++) ;
for (i = 0 ; i < n ; i++)
printf ("%s %s %d\n", stu[i].num, stu[i].name, stu[i].grade) ;
}
return 0 ;
}



posted @ 2012-01-06 23:54  Seraph2012  阅读(127)  评论(0编辑  收藏  举报