2021年度训练联盟热身训练赛第二场 D Soccer Standings 结构体 sort 排序

【2021年度训练联盟热身训练赛第二场】
D Soccer Standings (结构体 sort 排序)

今天比赛一道比较有意思的题目,要求根据足球比赛结果输出积分榜。使用结构体存储每支球队的数据,定义cmp函数以后使用sort排序即可。排序时需要考虑的条件比较多,要注意细节,也算是个模拟题了,写了八十多行。足球狗对310太熟悉了哈哈。继续努力。

【题目描述】
Soccer fever has gripped the world once again, and millions of people from dozens of countries
will be glued to their TV sets for the World Cup. Being an enterprising sort, you’ve started up your own internet World Cup Soccer Channel for streaming matches online. Recently you came up with the idea of filling up the time between matches by having a couple of ‘experts’ offer critical analysis of games. For this purpose, you have devised a unique ranking system for soccer teams, which you must now implement.
The Problem:
Given a list of teams and a list of match scores, you must compute several quantities for each team. These are: the total number of goals scored over all their games, the total number of goals scored against them (goals allowed, for short), the number of wins, draws and losses, and the number of points scored so far. Points are to be computed as follows: winning a match nets a team 3 points, losing gets them nothing. In the event of a tie, both teams get 1 point.
In addition to this, you must order the teams correctly according to your new system. Teams are ordered according to points, from highest to lowest. In the event of a tie in points, the team that has a higher goal difference comes first. The goal difference is defined as the total number of goals scored by the team minus the total number of goals scored against them.
If there is still a tie (i.e., two or more teams have the same points and the same goal differences), the team with higher total goals scored comes first. If even this is tied, the team whose name comes first in alphabetical order goes first.

输入描述:
The first input line contains a positive integer, n, indicating the number of data sets to be processed. The first line of each data set consists of two positive integers T (T ≤ 30) and G (G ≤
400) – the number of teams in this group and the total number of games played by them. The next line contains T unique names separated by single spaces. Each name is a single uppercase word with no more than 15 characters.
Each of the next G input lines will contain the results of a match. Each line is of the form
<country_1> <score_1> <country_2> <score_2>. For example, “Greece 2 Nigeria 1” indicates that Greece and Nigeria played a game with score 2-1. All four terms will be separated by single spaces.

输出描述:
At the beginning of output for each data set, output “Group g:” where g is the data set number, starting from 1. Next you should print a single line for each team, ordering teams as mentioned above. For each team, the line you print should be of the form “ ”. These items should be separated by single spaces. Leave a blank line after the output for each data set.

样例输入

2
2 1
KASNIA LATVERIA
KASNIA 0 LATVERIA 1
4 6
ENGLAND USA ALGERIA SLOVENIA
ENGLAND 1 USA 1
ALGERIA 0 SLOVENIA 1
SLOVENIA 2 USA 2
ENGLAND 0 ALGERIA 0
SLOVENIA 0 ENGLAND 1
USA 1 ALGERIA 0

样例输出

Group 1:
LATVERIA 3 1 0 0 1 0
KASNIA 0 0 1 0 0 1

Group 2:
USA 5 1 0 2 4 3
ENGLAND 5 1 0 2 2 1
SLOVENIA 4 1 1 1 3 3
ALGERIA 1 0 2 1 0 2

【AC代码】

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n,m;
struct node
{
	string name;
	int points,wins,losses,draws,scored,allowed;
}team[35];
bool cmp(node a,node b)
{
	if(a.points>b.points)
		return 1;
	if(a.points<b.points)
		return 0;
	if(a.scored-a.allowed>b.scored-b.allowed)
		return 1;
	if(a.scored-a.allowed<b.scored-b.allowed)
		return 0;
	if(a.scored>b.scored)
		return 1;
	if(a.scored<b.scored)
		return 0;
	if(a.name.compare(b.name)<0)//字典序a<b 
		return 1;
	return 0;
}
void update(string ss,int p,int s,int a)
{
	for(int i=0;i<n;i++)
		if(team[i].name==ss)
		{
			team[i].points+=p;
			team[i].scored+=s;
			team[i].allowed+=a;
			if(p==3)
				team[i].wins++;
			else if(p==1)
				team[i].draws++;
			else if(p==0)
				team[i].losses++;
			break;
		}
}
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T;
	cin>>T;
	for(int t=1;t<=T;t++)
	{
		cin>>n>>m;
		for(int i=0;i<n;i++)
		{
			cin>>team[i].name;
			team[i].points=team[i].wins=team[i].losses=team[i].draws=team[i].scored=team[i].allowed=0;
		}
		for(int i=0;i<m;i++)
		{
			string a,b;
			int c,d;
			cin>>a>>c>>b>>d;
			if(c>d)
			{
				update(a,3,c,d);
				update(b,0,d,c);
			}
			else if(c==d)
			{
				update(a,1,c,d);
				update(b,1,d,c);
			}
			else if(c<d)
			{
				update(a,0,c,d);
				update(b,3,d,c);
			}
		}
		sort(team,team+n,cmp);
		printf("Group %d:\n",t);
		for(int i=0;i<n;i++)
			printf("%s %d %d %d %d %d %d\n",team[i].name.c_str(),team[i].points,team[i].wins,team[i].losses,team[i].draws,team[i].scored,team[i].allowed);
		printf("\n");
	}
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

球王武磊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值