UVA 10194

The Problem

Football the most popular sport in the world (americans insist to call it "Soccer",but we will call it "Football"). As everyone knows, Brasil is the country that have mostWorld Cup titles (four of them: 1958, 1962, 1970 and 1994). As our national tournament have many teams(and even regional tournaments have many teams also) it's a very hard task to keep track ofstandings with so many teams and games played!

So, your task is quite simple: write a program that receives the tournament name, team namesand games played and outputs the tournament standings so far.

A team wins a game if it scores more goals than its oponent. Obviously, a team loses a gameif it scores less goals. When both teams score the same number of goals, we call it a tie.A team earns 3 points for each win, 1 point for each tie and 0 point for each loss.

Teams are ranked according to these rules (in this order):

 

  1. Most points earned.
  2. Most wins.
  3. Most goal difference (i.e. goals scored - goals against)
  4. Most goals scored.
  5. Less games played.
  6. Lexicographic order.

The Input

The first line of input will be an integer N in a line alone (0 < N < 1000). Then, will follow N tournament descriptions. Each one begins with the tournament name, on a single line. Tournament names can have any letter, digits, spaces etc. Tournament names willhave length of at most 100. Then, in the next line, there will be a number T (1 < T <= 30), which stands for the number of teams participating on this tournament. Then will follow T lines, each one containing one team name. Team names may have any character that have ASCII code greaterthan or equal to 32 (space), except for '#' and '@' characters, which will never appear in teamnames. No team name will have more than 30 characters.

Following to team names, there will be a non-negative integer G on a single line which stands forthe number of games already played on this tournament. G will be no greater than 1000. Then, Glines will follow with the results of games played. They will follow this format:

team_name_1#goals1@goals2#team_name_2

For instance, the following line:

Team A#3@1#Team B

Means that in a game between Team A and Team B, Team A scored 3 goals and Team B scored 1.All goals will be non-negative integers less than 20. You may assume that there will not be inexistent team names (i.e. all team names that appear on game results will have apperead on the team names section) and that no team will play against itself.

The Output

For each tournament, you must output the tournament name in a single line. In the next Tlines you must output the standings, according to the rules above. Notice that should thetie-breaker be the lexographic order, it must be done case insenstive. The output format for each line is shown bellow:

[a]) Team_name [b]p, [c]g ([d]-[e]-[f]), [g]gd ([h]-[i])

Where:

  • [a] = team rank
  • [b] = total points earned
  • [c] = games played
  • [d] = wins
  • [e] = ties
  • [f] = losses
  • [g] = goal difference
  • [h] = goals scored
  • [i] = goals against

There must be a single blank space between fields and a single blank line between output sets. See the sample output for examples.

Sample Input

 

2
World Cup 1998 - Group A
4
Brazil
Norway
Morocco
Scotland
6
Brazil#2@1#Scotland
Norway#2@2#Morocco
Scotland#1@1#Norway
Brazil#3@0#Morocco
Morocco#3@0#Scotland
Brazil#1@2#Norway
Some strange tournament
5
Team A
Team B
Team C
Team D
Team E
5
Team A#1@1#Team B
Team A#2@2#Team C
Team A#0@0#Team D
Team E#2@1#Team C
Team E#1@2#Team D

Sample Output

 

World Cup 1998 - Group A
1) Brazil 6p, 3g (2-0-1), 3gd (6-3)
2) Norway 5p, 3g (1-2-0), 1gd (5-4) 
3) Morocco 4p, 3g (1-1-1), 0gd (5-5)
4) Scotland 1p, 3g (0-1-2), -4gd (2-6)

Some strange tournament
1) Team D 4p, 2g (1-1-0), 1gd (2-1)
2) Team E 3p, 2g (1-0-1), 0gd (3-3)
3) Team A 3p, 3g (0-3-0), 0gd (3-3)
4) Team B 1p, 1g (0-1-0), 0gd (1-1)
5) Team C 1p, 2g (0-1-1), -1gd (3-4)

易错点:进球数看成一位,其实小于20,有可能是两位。

代码附上:

 

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
char tourName[102];
char name1[100];
char name2[100];
int team_num;
struct team{
	char teamName[35];
	int score;
	int wins;
	int ties;
	int losses;
	int goalScored;
	int goalAgainst;
}Team[35];

void init(int n){
	int i;
	for(i=1;i<=n;++i){
		Team[i].score = 0;
		Team[i].wins = 0;
		Team[i].ties = 0;
		Team[i].losses = 0;
		Team[i].goalScored = 0;
		Team[i].goalAgainst = 0;
	}
}

int find_name(char *name){
	int i;
	for(i=1;i<=team_num;++i)
		if(strcmp(name, Team[i].teamName) == 0)
			return i;
	return -1;
}

void jisuan(int p1, int p2, int score1, int score2){
	Team[p2].goalAgainst += score1;
	Team[p1].goalAgainst += score2; 
	Team[p1].goalScored += score1;
	Team[p2].goalScored += score2;
	if(score1 > score2){
		Team[p1].score+=3;
		Team[p1].wins++;
		Team[p2].losses++;
	}
	if(score1 < score2){
		Team[p2].score+=3;
		Team[p1].losses++;
		Team[p2].wins++;
	}
	if(score1 == score2){
		Team[p1].score++;
		Team[p2].score++;
		Team[p1].ties++;
		Team[p2].ties++;
	}
}

int _cmp(const void *_a, const void *_b){
	struct team *a = (struct team *)_a;
	struct team *b = (struct team *)_b;
	if(a->score != b->score) 
		return a->score < b->score;
	if(a->wins != b->wins) 
		return a->wins < b->wins;
	if((a->goalScored - a->goalAgainst) != (b->goalScored - b->goalAgainst))
		return (a->goalScored - a->goalAgainst) < (b->goalScored - b->goalAgainst);
	if(a->goalScored != b->goalScored) 
		return a->goalScored < b->goalScored;
	if( (a->wins + a->ties + a->losses) != (b->wins + b->ties + b->losses))
		return (a->wins + a->ties + a->losses) > (b->wins + b->ties + b->losses);
	return strcasecmp( a->teamName , b->teamName );
}

int main(int argc, char const *argv[])
{
	int t, i, g_num, p1, p2, score1, score2;
	char c;
	scanf("%d", &t);
	getchar();
	for(; 0 < t; --t){
		gets(tourName);
		scanf("%d", &team_num);
		getchar();
		init(team_num);
		for(i=1;i<=team_num;++i)	
			gets(Team[i].teamName);
		scanf("%d", &g_num);
		getchar();
		for(i=0;i<g_num;++i){
			p1 = p2 = 0;
			while((c = getchar())!= '#')
				name1[p1++] = c;
			name1[p1] = '';
			scanf("%d@%d#", &score1, &score2);
			/* 注释的这种也能AC,只是这种测试大量数据,会出现bus error 不清楚UVA为何可以AC。
			用gets(name2) 更好一点
			while((c = getchar()) != '
')
				name2[p2++] = c;
			name2[p2] = '';
			*/
			gets(name2);
			p1 = find_name(name1);
			p2 = find_name(name2);
			jisuan(p1, p2, score1, score2);
		}
		qsort(Team+1, team_num, sizeof(Team[0]), _cmp);
		puts(tourName);
		for(i=1;i<=team_num;++i){
			printf("%d) ", i);  
            printf("%s ", Team[i].teamName);  
            printf("%dp, ", Team[i].score);  
            printf("%dg ", (Team[i].wins + Team[i].ties + Team[i].losses));  
            printf("(%d-%d-%d), ", Team[i].wins, Team[i].ties, Team[i].losses);  
            printf("%dgd ", (Team[i].goalScored - Team[i].goalAgainst));  
            printf("(%d-%d)
", Team[i].goalScored, Team[i].goalAgainst);
		}
		if(t > 1) printf("
");
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/firstrate/p/3541662.html