HDU 2115 -I Love This Game

 1 #include<stdio.h>

 2 #include<string.h>

 3 #include<stdlib.h>//包含qsort函数的头文件 

 4 struct node

 5 {

 6     char name[20],time[6];

 7     int t;//时间 

 8     int rank;//排名 

 9 }p[11];

10 int cmp(const void *a,const void *b)

11 {

12     struct node *c=(struct node *)a;

13     struct node *d=(struct node *)b;

14     if(c->t!=d->t)

15       {return c->t-d->t;}//时间比较

16     else

17       {return strcmp(c->name,d->name);}//名字字典排序

18 }

19 int main()

20 {

21     int n,i,j,count,ranklist,flag,m;

22     count=0;

23     m=0;

24     while(scanf("%d",&n)!=EOF)

25     {

26         if(n==0)

27           {break;}

28         if(m!=0)

29           {printf("\n");}

30            m++;

31         for(i=0;i<n;i++)

32           {

33              getchar();

34              scanf("%s%s",p[i].name,p[i].time);

35              p[i].t=((p[i].time[0]-'0')*10+(p[i].time[1]-'0'))*60+(p[i].time[3]-'0')*10+(p[i].time[4]-'0');

36           }

37         qsort(p,n,sizeof(p[0]),cmp);

38         

39         printf("Case #%d\n",++count);

40         ranklist=1;

41         for(i=0;i<n;i++)//时间相同的运动员计数 

42         {

43             flag=i;// 标记时间相同的运动员的起始下标;

44             while(p[i].t==p[i+1].t)

45               {i++;} 

46             for(j=flag;j<=i;j++)

47               p[j].rank=ranklist;//给排名赋值0

48             ranklist+=j-flag;

49         }

50         for(i=0;i<n;i++)

51         {

52             printf("%s %d\n",p[i].name,p[i].rank);

53         }

54     }

55     return 0;

56 }

57         

你可能感兴趣的:(this)