hdu2115: http://acm.hdu.edu.cn/showproblem.php?pid=2115
题意:输入n组名字和对应的时间(分:秒),要求按时间长度由短到长排序,并输出对应排名,若时间一样,则按名字字典序排序,名次可以并列。
code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
struct name
{
char v[200],w[200];
int x;
int ans;
};
bool cmp(name a,name b) //第一关键字为时间,第二关键字为字符串,若时间相同,字典序排序
{
if(a.x==b.x)
return strcmp(a.w,b.w)<0;
return a.x<b.x;
}
int main()
{
int n,cas=0;
while(1)
{
name kk[20];
scanf("%d",&n);
cas++;
if(n==0)break;
for(int i=0;i<n;i++)
{
scanf("%s%s",kk[i].v,kk[i].w);
}
for(int i=0;i<n;i++) //计算时间
{
kk[i].x=((kk[i].w[0]-'0')*10+(kk[i].w[1]-'0'))*60+(kk[i].w[3]-'0')*10+(kk[i].w[4]-'0');
}
sort(kk,kk+n,cmp);
int ans1=1;
kk[0].ans=1;
for(int i=1;i<n;i++)
{
if(kk[i].x==kk[i-1].x)
kk[i].ans=ans1++;
else
kk[i].ans=++ans1;
}
if(cas!=1)
printf("\n");
printf("Case #%d\n",cas);
for(int i=0;i<n;i++)
{
printf("%s %d\n",kk[i].v,kk[i].ans);
}
}
}
/*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
output:
Case #1
Bryant 1
Wade 1
Nash 3
Davies 4
Carter 5
Iverson 6
James 7
Parker 8
Kidd 9
Jordan 10
*/