HDU 2115 I Love This Game

题目HDU 2115:http://acm.hdu.edu.cn/showproblem.php?pid=2115

Problem Description

Do you like playing basketball ? If you are , you may know the NBA Skills Challenge . It is the content of the basketball skills . It include several parts , such as passing , shooting , and so on. After completion of the content , the player who takes the shortest time will be the winner . Now give you their names and the time of finishing the competition , your task is to give out the rank of them ; please output their name and the rank, if they have the same time , the rank of them will be the same ,but you should output their names in lexicographic order.You may assume the names of the players are unique.

Is it a very simple problem for you? Please accept it in ten minutes.

Input

This problem contains multiple test cases! Ease test case contain a n(1<=n<=10) shows the number of players,then n lines will be given. Each line will contain the name of player and the time(mm:ss) of their finish.The end of the input will be indicated by an integer value of zero.

Output

1.The output format is shown as sample below.
2.Please output the rank of all players, the output format is shown as sample below;
3.Output a blank line between two cases.

解题思路

这道题“Is it a very simple problem for you? Please accept it in ten minutes.”就真的有点小过分了,自己还需提升水平只能这样子讲。
这道题,确实很好实现,利用sort()函数对数组结构体进行排序,最后进行判断一下就行了。在输入方面,即读到0的时候就可以停止了,所以可以利用

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

但是这里有个输出格式的小陷阱,题目中说了Output a blank line between two cases.即在你第二次输入的时候需要加一行空行,这但很容易被忽视,注意到这些基本上就可以AC了!直接放出代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;

struct player{
char name[100];
int num;
int time;
}a[12];

 int compare(struct player x,struct player y)
  {
   return (x.time==y.time?  strcmp(x.name,y.name)>1: x.time<y.time);  /*如果时间相同,就按字母对名字排序;若不同,则按照升序进行排列*/
  }

int main()
{
    int n,i,j=1;
    while(scanf("%d",&n)!=EOF,n)
    {
        int m,s;
        for(i=0;i<n;i++)
            {
            scanf("%s %d:%d",a[i].name,&m,&s);
            a[i].time=60*m+s;
            }
        sort(a,a+n,compare);        /*sort()用于将结构体内的内容按照compare所鬼灯的法则进行排序*/
        if(j!=1) printf("\n");        /*题目说了:Output a blank line between two cases*/
        printf("Case #%d\n",j++);
        for(i=0;i<n;i++)
        {if(i>0&&(a[i].time==a[i-1].time)) /*如果两个时间相等,就编号一样;若两个时间不相等,进行正确的排序*/
              a[i].num=a[i-1].num;
         else
              a[i].num=i+1;
         printf("%s %d\n",a[i].name,a[i].num);
        }
    }
   return 0;
}
posted @ 2017-09-26 15:50  MrYun  阅读(130)  评论(0编辑  收藏  举报