【杭电oj】1225 - Football Score(结构体排序)

Football Score

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3448    Accepted Submission(s): 972


Problem Description
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.
 

Input
The 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 – 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.
 

Output
For 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 – 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
Hint
Hint
Huge input, scanf is recommended.
 

Author
Gao Bo
 

Source
杭州电子科技大学第三届程序设计大赛


难倒是不难的,就是细节问题太重要了,总是在小地方弄错。注释里标的地方注意一下,AC还是可以的。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int num;
struct node
{
	char name[55];
	int win,lost,net;
	int grade;
}data[1000];
int find (char a[])
{
	for (int i = 0 ; i < num ; i++)
	{
		if (strcmp (a , data[i].name) == 0)
			return i;
	}
	return -1;		//查无此人 
}
bool cmp(node a,node b)
{
	if (a.grade != b.grade)
		return a.grade > b.grade;
	else if (a.net != b.net)
		return a.net > b.net;
	else if (a.win != b.win)
		return a.win > b.win;
	else
	{
		int t = strcmp (a.name , b.name);
		if (t == -1)		//字典序 
			return true;
		else
			return false;
	}
}
int main()
{
	int n;
	char name1[55];
	char name2[55];
	int t1,t2;		//比分 
	while (~scanf ("%d",&n))
	{
		num = 0;
		memset (data,0,sizeof (data));
		int tn = n * (n - 1);
		char VS[3];
		for (int i = 0 ; i < tn ; i++)
		{
			scanf ("%s %s %s %d:%d",name1,VS,name2,&t1,&t2);
			int t = find (name1);
			if (t == -1)
			{
				strcpy (data[num].name , name1);
				data[num].win = t1;
				data[num].lost = t2;
				data[num].net = t1 - t2;
				if (t1 > t2)
					data[num].grade = 3;
				else if (t1 == t2)
					data[num].grade = 1;
				num++;
			}
			else
			{
				data[t].win += t1;
				data[t].lost += t2;
				data[t].net += (t1 - t2);
				if (t1 > t2)
					data[t].grade += 3;
				else if (t1 == t2)
					data[t].grade += 1;
			}
			
			t = find (name2);
			if (t == -1)
			{
				strcpy (data[num].name , name2);
				data[num].win = t2;
				data[num].lost = t1;
				data[num].net = t2 - t1;
				if (t2 > t1)
					data[num].grade = 3;
				else if (t1 == t2)
					data[num].grade = 1;
				num++;
			}
			else
			{
				data[t].win += t2;
				data[t].lost += t1;
				data[t].net += (t2 - t1);
				if (t2 > t1)		//这里赋值粘贴时记得别写反了(惨痛的教训) 
					data[t].grade += 3;
				else if (t1 == t2)
					data[t].grade += 1;
			}
		}
		sort (data , data + n , cmp);
		for (int i = 0 ; i < n ; i++)
			printf ("%s %d\n",data[i].name,data[i].grade);
		printf ("\n");		//少写个换行符 = = 注意注意 
	}
	return 0;
}


你可能感兴趣的:(【杭电oj】1225 - Football Score(结构体排序))