hdu2115: http://acm./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++;elsekk[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:10Iverson 17:19Bryant 07:03Nash 09:33Wade 07:03Davies 11:13Carter 14:28Jordan 29:34James 20:48Parker 24:49Kidd 26:460output:Case #1Bryant 1Wade 1Nash 3Davies 4Carter 5Iverson 6James 7Parker 8Kidd 9Jordan 10*/