A - I Love This Game HDU - 2115

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.

InputThis 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.
OutputThe output format is shown as sample below.
Please output the rank of all players, the output format is shown as sample below;
Output a blank line between two cases.Sample Input

10
Iverson 17:19
Bryant 07:03
Nash 09:33
Wade 07:03
Davies 11:13
Carter 14:28
Jordan 29:34
James 20:48
Parker 24:49
Kidd 26:46
0

Sample Output

Case #1
Bryant 1
Wade 1
Nash 3
Davies 4
Carter 5
Iverson 6
James 7
Parkr 8
Kidd 9
Jordan 10


主要:设计思路,利用结构体和时间的的两个单位都化为秒。
   这道题主要考察,输出格式(每个case之间有空行)相同名次的输出,和sort排列的使用。
代码如下:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct H
{
 string name;
 int num;
};
H h[15];
bool cmp(H a, H b)
{
 if (a.num == b.num) return a.name < b.name;
 return a.num<b.num;
}
int main()
{
 int num,time,t=1,ans=0;
 while (cin >> num)
 {
  if (num == 0) break;
  for (int i = 0; i < num; i++)
  {
   cin >> h[i].name;
   cin >> time;
   time *= 60;
   h[i].num = time;
   getchar();
   cin >> time;
   h[i].num += time;
  }
  sort(h, h + num, cmp);

  if (ans) cout << endl;
  cout << "Case #" << t++ << endl;
  int k ,p=0;
  k = 1;
  for (int i = 0; i < num; i++)
  {
   if (h[i].num == h[i - 1].num&&i!=0)
   {
    p++;
    cout << h[i].name << " " << k-p << endl;
   }
   else
   {
    p = 0;
    cout << h[i].name << " " << k << endl;
   }
   k++;
  }
  ans = 1;
 }
 return 0;
}

 

注意:每个人都有不同的方式表达逻辑方法和习惯,比如我比较喜欢手画出来,我们多用更熟悉的方式来ac。

    作者在做这道题的时候,想象名次的输出的时候wa了多次,但是,画一画马上就有很清晰的逻辑关系了。

    也算是自己的一点儿小经验吧。

转载于:https://www.cnblogs.com/damaoranran/p/8778225.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值