G - 请使用sort

请使用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.


思路:

题意:

输入数字n,然后接下来n*(n-1)行,输入两个队伍比赛得分信息,格式为team1 VS team2 p:q ,输出每个队伍的得分情况;

输出顺序要求:

总得分不一样的,得分高的先输出;
总得分一样的,分差score net(p-q/q-p)高的先输出;
分差一样的,进球数多的先输出;
进球数一样的,按队伍名的字母顺序输出;
坑:
输入的信息和格式要求比较多,输入和保存形式是个问题;【C++ String 用法
sort 排序要求多 【结构体sort排序


代码

#include <iostream>
#include <algorithm>
#include <string>
#include <map>

using namespace std;

struct Node
{
    string name;
    int score,goal,net;//分别代表得分,进球数,分差
}arr[1005];

bool cmp(Node x,Node y)
{
    if(x.score!=y.score)
        return x.score>y.score;

    else if(x.net!=y.net)
        return x.net>y.net;

    else if(x.goal!=x.goal)
        return x.goal>y.goal;

    else return x.name<y.name;//名字采用升序
}
int main()
{
    int i;
    int n,p,q,num;
    string team1,vs,team2;
    char a;//消化":"

    while(cin>>n){
        map<string,int>m;
        for(i=0;i<=n*(n-1);i++){
            arr[i].goal=arr[i].net=arr[i].score=0;
        }

        num=1;

        for(i=0;i<n*(n-1);i++){

            cin>>team1>>vs>>team2>>p>>a>>q;

            if(m[team1]==0){
            m[team1]=num;
            arr[num].name=team1;
            num++;
            }
            arr[m[team1]].net+=p-q;
            arr[m[team1]].goal+=p;

            if(m[team2]==0){
            m[team2]=num;
            arr[num].name=team2;
            num++;
            }
            arr[m[team2]].net+=q-p;
            arr[m[team2]].goal+=q;

            if(p>q)
                arr[m[team1]].score+=3;
            else if(p==q){
                arr[m[team1]].score+=1;
                arr[m[team2]].score+=1;
            }
            else arr[m[team2]].score+=3;

        }

        sort(arr+1,arr+1+n,cmp);

        for(i=1;i<=n;i++)
            cout<<arr[i].name<<" "<<arr[i].score<<endl;

        cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值