A - I Love This Game HDU - 2115
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了多次,但是,画一画马上就有很清晰的逻辑关系了。
也算是自己的一点儿小经验吧。