2013
12-29

I Love This Game

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.

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.

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.

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

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

这道题目一看就知道是用结构体做，因为有两个限制条件，

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>//用字符串string来比较大小一定要加这个头文件
using namespace std;
struct p
{
string name;
int  hour;
int fen;
int time;
int paiming;
} f[11];

int cmp(p x, p y)
{
if(x.hour==y.hour)
{
if(x.fen==y.fen)
{
//return strcmp(x.name,y.name)<0?1:0;
return x.name<y.name;
}
else
return x.fen<y.fen;
}
else
return x.hour<y.hour;
}
int main()
{
int n,i;
int tcase=1;
while(scanf("%d",&n),n)
{
for(i=0; i<n; i++)
{
cin>>f[i].name;
scanf("%d:%d",&f[i].hour,&f[i].fen);
f[i].time=60*f[i].hour+f[i].fen;
}
sort(f,f+n,cmp);
f[0].paiming=1;
for(i=1; i<n; i++)
{
if(f[i].time==f[i-1].time)
{
f[i].paiming=f[i-1].paiming;
}
else
f[i].paiming=i+1;

}
if(tcase!=1)
{
printf("\n");
}
printf("Case #%d\n",tcase++);
for(i=0; i<n; i++)
{
cout<<f[i].name<<" ";
printf("%d\n",f[i].paiming);
}
}
return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
struct p
{
string name;
int  hour;
int fen;
int time;
int paiming;
} f[11];

int cmp(p x, p y)
{
if(x.hour==y.hour)
{
if(x.fen==y.fen)
{
return strcmp(x.name,y.name)<0?1:0;//这里如果两个字符串相等的话就==0，如果第一个字符串比第二个字符串小==-1，如果第一个字符串比第二个字符串大==1；这是strcmp（）函数
}
else
return x.fen<y.fen;
}
else
return x.hour<y.hour;
}
int main()
{
int n,i;
int tcase=1;
while(scanf("%d",&n),n)
{
for(i=0; i<n; i++)
{
cin>>f[i].name;
scanf("%d:%d",&f[i].hour,&f[i].fen);
f[i].time=60*f[i].hour+f[i].fen;
}
sort(f,f+n,cmp);
f[0].paiming=1;
for(i=1; i<n; i++)
{
if(f[i].time==f[i-1].time)
{
f[i].paiming=f[i-1].paiming;
}
else
f[i].paiming=i+1;

}
if(tcase!=1)
{
printf("\n");
}
printf("Case #%d\n",tcase++);
for(i=0; i<n; i++)
{
cout<<f[i].name<<" ";
printf("%d\n",f[i].paiming);
}
}
return 0;
}

1. 算法是程序的灵魂，算法分简单和复杂，如果不搞大数据类，程序员了解一下简单点的算法也是可以的，但是会算法的一定要会编程才行，程序员不一定要会算法，利于自己项目需要的可以简单了解。