G - 请使用sort

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


        
  
Huge input, scanf is recommended.
Hint
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;//不可缺少 
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值