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.
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.
Process to the end of file.
Output a blank line after each test case.
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
Manchester 8
Portsmouth 5
Liverpool 2
Huge input, scanf is recommended.
Hint
看了半天的题目才知道大致意思是输入n个球队,然后输入n*(n-1)个比赛,最后输出队名和得分,输出按总得分降序输出。如果得分一样,按进球与失球只差降序输出,如果又一样,按进球数降序输出,再一样就按队名谁短谁先输出。之前没写过这样的题目,不知道怎么下手,百度了许多博客,最后发现一个学过但又很厉害的方法,看了半天,软件写崩了最后才写出来,还发现了个新问题:vj 二进制“>>”: 没有找到接受“std::string”类型的右操作数的运算符。 这就需要吧#include<cstring>改为#include<string>,很有收益的一题。还学到了c语言中sort函数,它是类似于快排算法。还有map函数老师讲过,当作复习了。
#include<iostream> #include<algorithm> #include<map> #include<string> #include<cstdio> using namespace std; struct node { string name; //string函数是c++一个可以存储字符串的函数。 int score,differ,goal;//分别表示总分,进失差值,进球得分。 }group[1000]; bool cmp(node x,node y)//实现降序。sort默认升序。 { if(x.score!=y.score) { return x.score>y.score; } if(x.differ!=y.differ) { return x.differ>y.differ; } if(x.goal!=y.goal) { return x.goal>y.goal; } return x.name<y.name;//名字要升序 } int main() { int n,i,temp1,temp2,num; string team1,vs,team2;//暂时存储每一次比赛的队名。 char a; while(cin>>n) { map<string,int>m;//map函数 num=1; for(i=1;i<=n*(n-1);i++)//从1开始是因为编号不能为0。 { group[i].score=0; group[i].differ=0; group[i].goal=0; } for(i=0;i<n*(n-1);i++) { cin>>team1>>vs>>team2>>temp1>>a>>temp2; if(m[team1]==0)//标记是否出现过,没出现过就编编号。 { m[team1]=num; group[num].name=team1; num+=1; } group[m[team1]].differ+=temp1-temp2; group[m[team1]].goal+=temp1; if(m[team2]==0) { m[team2]=num; group[num].name=team2; num+=1; } group[m[team2]].differ+=temp2-temp1; group[m[team2]].goal+=temp2; if(temp1>temp2) { group[m[team1]].score+=3; } else if(temp1==temp2) { group[m[team1]].score+=1; group[m[team2]].score+=1; } else { group[m[team2]].score+=3; } } sort(group+1,group+n+1,cmp); for(i=1;i<=n;i++) { cout<<group[i].name<<" "<<group[i].score<<endl; } cout<<endl;//不可缺少 } }