HDU 1225 Football Score

来源:互联网 发布:移动数据终端 编辑:程序博客网 时间:2024/05/19 18:41

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1225

Football Score

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


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
3Manchester VS Portsmouth 3:0Liverpool VS Manchester 1:1Liverpool VS Portsmouth 0:0Portsmouth VS Manchester 1:1Manchester VS Liverpool 2:1Liverpool VS Portsmouth 1:2
 

Sample Output
Manchester 8Portsmouth 5Liverpool 2
题目大意:给出n个球队的每次比赛的比分。最后输出球队名和最后总得分 ,输出按总得分降序排序(从大到小输出)。如果总分相同。则按进球数与失球数之差降序排序,如果进球数与失球数之差相等,则按进球数降序排序,如果进球数也相同,那就按队名字典序升序排序。


这只是一道水题,但是我在做的时候遇到了问题。就是我用memset函数对含有string类型的结构体初始化,编译虽然能通过,但是总是运行错误,一开始根本不知道这个东西,纠结了一个晚上,我的代码明明没错,但是为什么会运行错误呢?后来一步步地调试,就在对结构体中string类型变量赋值的时候出现问题了。后来我去网上百度了一下,看到有人说memset不能初始化string,会导致错误,我这才醒悟。道理很简单,memset是C语言里的东西,string是C++里的东西,C语言出现的比较早不可能向后兼容。


水题也有水题的价值呀,能让你发现各种细节问题。还希望各位看官不要眼高手低,要多动手!

由于字符串不能做数组下标,而STL中的map可以实现这一点。

下面是AC代码

#include<iostream>#include<algorithm>#include<map>#include<cstring>#include<cstdio>using namespace std;struct node{    string name;    int s1,s2,s3;}a[100005];bool cmp(node x,node y){    if(x.s1!=y.s1) return x.s1>y.s1;    if(x.s2!=y.s2) return x.s2>y.s2;    if(x.s3!=y.s3) return x.s3>y.s3;    return x.name<y.name;}int main(){    int n,p,q;    string na,op,nb;    char c;    while(cin>>n)    {        map<string,int>m;        int cnt=0;        //memset(a,0,sizeof(a));//memset  string is error        for(int i=1;i<=n*(n-1);i++) a[i].s1=0,a[i].s2=0,a[i].s3=0;        for(int i=0;i<n*(n-1);i++)        {            cin>>na>>op>>nb>>p>>c>>q;            if(m[na]==0)//此字符串没有出现过            {                m[na]=++cnt;//则对其编号                a[cnt].name=na;            }            a[m[na]].s2+=p-q;            a[m[na]].s3+=p;            if(m[nb]==0)            {                m[nb]=++cnt;                a[cnt].name=nb;            }            a[m[nb]].s2+=q-p;            a[m[nb]].s3+=q;            if(p>q) a[m[na]].s1+=3;            else if(p==q) a[m[na]].s1+=1,a[m[nb]].s1+=1;            else a[m[nb]].s1+=3;        }        sort(a+1,a+n+1,cmp);        for(int i=1;i<=n;i++)            cout<<a[i].name<<" "<<a[i].s1<<endl;        cout<<endl;    }    return 0;}


0 0
原创粉丝点击