UVa OJ
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 most World 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 of standings with so many teams and games played!
So, your task is quite simple: write a program that receives the tournament name, team names and 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 game if 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):
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 will have 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 greater than or equal to 32 (space), except for '#' and '@' characters, which will never appear in team names. 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 for the number of games already played on this tournament. G will be no greater than 1000. Then, G lines will follow with the results of games played. They will follow this format:
team_name_1#goals1@goals2#team_name_2For instance, the following line:
Team A#3@1#Team BMeans 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.
For each tournament, you must output the tournament name in a single line. In the next T lines you must output the standings, according to the rules above. Notice that should the tie-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:
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
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)
首先是输入有多组测试数据。 每组数据当中有n支队伍,接下来给你n行,每行代表一支队伍。之后是T场比赛 ,每场比赛的格式为:Team E#1@2#Team D 即Team E 1:2 Team D
规定没胜一场积分加3,平局加1,输了不加分
之后就是按照给定的规则对这些球队进行排序
先按照分数高低排序,然后按照胜的场次排,然后是净胜球(即总进球数减去丢球数),然后是总进球数,然后是比赛的场次(越少排越前面),最后是按照球队名称按照字典顺序排,不分大小写
其实这道题目的思想很简单,就是字符串处理+给定规则排序。不过处理起来比较麻烦。然后我学习了别人的写法,其中用到了STL中的map(我还一直没有去看,发现很好用!)还有strcasecmp(不分字母大小写进行比较),sscanf()
我自己敲了一遍,wa了很多次,后来发现是输出时多了个逗号。。。。。。。。悲剧
这里不写关于sscanf(),STL (map)了,毕竟是基础,准备另外写一篇。
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<ctype.h> #include<map> using namespace std; struct node { char name[50]; int score,played,wins,ties,loses,in,out; void fun(int a,int b) { played++; if (a>b) { wins++; score+=3; } else if (a==b) { ties++; score+=1; } else loses++; in+=a; out+=b; } } team[40]; bool cmp(node s, node v) { if (s.score!=v.score) return s.score>v.score; else if (s.wins!=v.wins) return s.wins>v.wins; else if ((s.in-s.out)!=(v.in-v.out)) return ((s.in-s.out)>(v.in-v.out)); else if (s.in!=v.in) return (s.in>v.in); else if (s.played!=v.played) return (s.played<v.played); else return strcasecmp(s.name,v.name)<0;//strcasecmp函数,在比较两个字符串时,忽略大小写 } int main () { int n,i,j,t,k; cin>>n; getchar(); for (k=0; k<n; k++) { char tour[110]; map<string,int>games; //定义一个map,以string为关键字 memset(team,0,sizeof(team)); gets(tour); cin>>t; getchar(); char temp[50]; for (i=0; i<t; i++) { gets(temp); games.insert(pair<string,int> (temp,i));//往map里面添加数据 strcpy(team[i].name,temp); } int g; cin>>g; getchar(); char str[1000]; while(g--) { char name1[50], name2[50]; int score1,score2; gets(str); sscanf(str,"%[^#]#%d@%d#%[^\n]",name1,&score1,&score2,name2);//sscanf函数,可以分割字符串,跳过忽略的字符 //printf("%s %d %d %s\n",name1,score1,score2,name2); team[games[name1]].fun(score1,score2);//games[name1]是调用了map函数,通过name1找到了对应的int team[games[name2]].fun(score2,score1);//结构体中还可以定义函数,把程序简单化 } sort(team,team+t,cmp); if (k!=0) cout<<endl; puts(tour); for (i=0; i<t; i++) printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",i+1,team[i].name,team[i].score,team[i].played,team[i].wins,team[i].ties,team[i].loses,team[i].in-team[i].out,team[i].in,team[i].out);//之前在输出的时候多了个,。。wa了好多次 } return 0; }