您的位置:首页 > 其它

2016 小灶1 C题 (HDU1225)

2017-01-14 22:28 369 查看
题目:

Football is one of the greatest games in the world. Lots of people like to play football. After one season of matches, the header has to calculate the last scores of every team. He is too lazy that he doesn't want to calculate, so
he asks you to write a program for him to solve the problem.

Here are the rules:

1 Every team has to match with all the other teams.

2 Every two teams have to match for two times,one at home and one away.

3 In one match, the winner will get 3 points, the loser will get 0 point. If it is draw, both of them will get 1 point.

InputThe input consists of many test cases. In each case, there will be a number N in the first line which means the number of teams. Followed by N * (N �C 1)lines. Each line stands for a match between two teams. The format is: "Team1 VS Team2 p:q", p stands
for the balls that Team1 has kicked in and q stands for the balls that Team2 has kicked in. p and q are not greater than 9.

Process to the end of file.
OutputFor each test case, output the teams and their scores in descending order. One line a team, the format is: "TeamX scores". If two teams get the same score, the one with high net goals will be ahead, which net goal means the difference between the total
balls that the team kicked in and the total balls that the team lost. IE: if one team kicked in 30 balls and lost 40 balls, then the net goal is 30 �C 40 = -10. If two teams have the same score and the same net goal, the one whose kicked in balls is bigger
will be ahead. If two teams have the same score and the same net goal and the same kicked in balls, they will be outputed in alphabetic order.

Output a blank line after each test case.
Sample Input
3
Manchester VS Portsmouth 3:0
Liverpool VS Manchester 1:1
Liverpool VS Portsmouth 0:0
Portsmouth VS Manchester 1:1
Manchester VS Liverpool 2:1
Liverpool VS Portsmouth 1:2

Sample Output
Manchester 8
Portsmouth 5
Liverpool 2 

方法:用STL中的sort解题源代码:
//By Sean Chen
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef struct    //用结构体记录球队信息
{
string name;
int score,netscore,kickscore;
}Match;

int cmp(Match a,Match b)    //定义sort中的排序方法
{
if(a.score!=b.score)
return a.score>b.score;
else
{
if(a.netscore!=b.netscore)
return a.netscore>b.netscore;
else
{
if(a.kickscore!=b.kickscore)
return a.kickscore>b.netscore;
else
return a.name<b.name;
}
}
}
int main()
{
int N,s1,s2,d1,d2,i;
Match team[100];
string t1,t2,temp;
char c;
while(cin>>N)
{
int k=0;
memset(team,0,sizeof(team));
int T=(N-1)*N;
while(T--)
{
cin>>t1>>temp>>t2>>s1>>c>>s2;
if(s1>s2)                    //赢,平,负三种情况下的分数
{d1=3;d2=0;}
else if(s1<s2)
{d1=0;d2=3;}
else
{d1=1;d2=1;}

for( i=0;i<k;i++)
if(t1==team[i].name)
{
team[i].score+=d1;
team[i].netscore+=(s1-s2);
team[i].kickscore+=s1;
break;
}
if(i==k)
{
team[k].name=t1;
team[k].score+=d1;
team[k].netscore+=(s1-s2);
team[k].kickscore+=s1;
k++;
}

for(i=0;i<k;i++)
if(t2==team[i].name)
{
team[i].score+=d2;
team[i].netscore+=(s2-s1);
team[i].kickscore+=s2;
break;
}
if(i==k)
{
team[k].name=t2;
team[k].score+=d2;
team[k].netscore+=(s2-s1);
team[k].kickscore+=s2;
k++;
}
}
sort(team,team+N,cmp);
for(i=0;i<N;i++)
{cout<<team[i].name<<" "<<team[i].score<<endl;
}
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: