作业训练一编程题17. 世界杯来了

【问题描述】

2018年俄罗斯世界杯结束了,法国获得冠军,全世界球迷度过了一个非常愉快的夏天。作为中国球迷,不能总是看别人踢球,这不福利来了,根据FIFA(国际足联)及全体成员协会的一致决定,2118年世界杯将在中国举办,作为东道主,中国队将无需参加预选赛而直接参加决赛阶段的比赛。

比赛规则如下:

总共n(n为偶数)个球队参加比赛

按照分组赛积分排名,前n/2的球队进入淘汰赛

积分排名的规则如下:球队获胜得3分,平局得1分,失利得0分,按照积分递减、净胜球递减以及进球数递减方式排名

编写一个程序,根据给出的参赛队伍名单和所有比赛的结果,找出成功进入淘汰赛阶段的球队名单。

【输入形式】

第一行输入包含唯一整数n(1<=n<=50),参加世界杯决赛的球队数量。接下来的n行是各球队的名字,为长度不超过30个字符的英文字符。接下来的n*(n-1)/2行,每行格式name1-name2 num1:num2(0<=num1, num2<=100),表示对阵球队及比分.

【输出形式】

输入n/2行,表示进入淘汰赛阶段的球队,按照字典序进行排列,每个球队名字占一行。

【样例输入】

4

A

B

C

D

A-B 1:1

A-C 2:2

A-D 1:0

B-C 1:0

B-D 0:3

C-D 0:3

【样例输出】

A

D

#include

#include

#include

#include

using namespace std;

struct team {

public:

int score = 0;

int winBalls = 0;

int goalDifference = 0;

team() = default;

team(int s, int g, int w) {

score = s;

goalDifference = g;

winBalls = w;

}

};

struct compare {

bool operator()(const pair &a, const pair &b) {

if (a.second.score != b.second.score) return a.second.score > b.second.score;

else if (a.second.goalDifference != b.second.goalDifference)

return a.second.goalDifference > b.second.goalDifference;

else return a.second.winBalls > b.second.winBalls;

}

};

int main() {

int n, lBalls, rBalls;

string tmp;

map m;

cin >> n;

int rounds = n * (n - 1) / 2;

string res[n / 2];

for (int i = 0; i < n; i++) {

cin >> tmp;

m.emplace(tmp, team(0, 0, 0));

}

for (int i = 0; i < rounds; i++) {

string lTeamName, rTeamName;

cin.get();

char ch;

while (ch = cin.get()) {

if (ch != '-' && ch != ' ') lTeamName += ch;

else break;

}

while (ch = cin.get()) {

if (ch != '-' && ch != ' ') rTeamName += ch;

else break;

}

cin >> lBalls >> ch >> rBalls;

if (lBalls > rBalls) {

m[lTeamName].score += 3;

m[lTeamName].goalDifference += lBalls - rBalls;

m[rTeamName].goalDifference += rBalls - lBalls;

m[lTeamName].winBalls += lBalls;

m[rTeamName].winBalls += rBalls;

} else if (lBalls == rBalls) {

m[lTeamName].score++;

m[rTeamName].score++;

m[lTeamName].winBalls += lBalls;

m[rTeamName].winBalls += rBalls;

} else {

m[rTeamName].score += 3;

m[lTeamName].goalDifference += lBalls - rBalls;

m[rTeamName].goalDifference += rBalls - lBalls;

m[lTeamName].winBalls += lBalls;

m[rTeamName].winBalls += rBalls;

}

}

vector> v(m.begin(), m.end());

sort(v.begin(), v.end(), compare());

for (int i = 0; i < n / 2; ++i) res[i] = v[i].first;

sort(res, res + n / 2);

for (const auto &item: res) cout << item << endl;

return 0;

}