Leetcode :模拟足球赛小组各种比分的出线状况
题目
题描述
在一个足球小组赛中,有4支队伍(A、B、C、D)进行单循环比赛。每两支队伍之间进行一场比赛,共进行6场比赛。比赛结果以二维整数数组的形式给出,表示每场比赛的比分。
请你根据比赛结果,确定哪两支队伍能够出线(晋级到下一轮)。
出线规则(按优先级排序):
积分高者排名靠前:胜一场得3分,平一场得1分,负一场得0分
净胜球多者排名靠前:净胜球 = 进球数 - 失球数
进球数多者排名靠前
字母顺序靠前者排名靠前:如果以上条件都相同,按队伍名称的字母顺序排列
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <windows.h>using namespace std;// 设置控制台编码为UTF-8
void setConsoleUTF8() {
#ifdef _WIN32SetConsoleOutputCP(65001);
#endif
}class Team {
public:string name;int points;int goalsFor;int goalsAgainst;int goalDifference;Team(string n) : name(n), points(0), goalsFor(0), goalsAgainst(0), goalDifference(0) {}void updateStats(int scored, int conceded) {goalsFor += scored;goalsAgainst += conceded;goalDifference = goalsFor - goalsAgainst;if (scored > conceded) points += 3;else if (scored == conceded) points += 1;}void reset() {points = 0;goalsFor = 0;goalsAgainst = 0;goalDifference = 0;}
};bool compareTeams(const Team &a, const Team &b) {if (a.points != b.points) return a.points > b.points;if (a.goalDifference != b.goalDifference) return a.goalDifference > b.goalDifference;if (a.goalsFor != b.goalsFor) return a.goalsFor > b.goalsFor;return a.name < b.name;
}vector<string> calculateQualifiedTeams(const vector<vector<int>>& matches) {vector<Team> teams = {Team("A"), Team("B"), Team("C"), Team("D")};vector<pair<int, int>> fixtures = {{0,1}, {0,2}, {0,3}, {1,2}, {1,3}, {2,3}};for (int i = 0; i < matches.size() && i < fixtures.size(); i++) {if (matches[i].size() < 2) continue;int t1 = fixtures[i].first;int t2 = fixtures[i].second;int s1 = matches[i][0];int s2 = matches[i][1];teams[t1].updateStats(s1, s2);teams[t2].updateStats(s2, s1);}sort(teams.begin(), teams.end(), compareTeams);return {teams[0].name, teams[1].name};
}bool runTest(const vector<vector<int>>& matches, const vector<string>& expected, const string& testName) {vector<string> result = calculateQualifiedTeams(matches);bool passed = (result == expected);cout << testName << ": " << (passed ? "PASS" : "FAILED") << endl;if (!passed) {cout << " 期望: " << expected[0] << ", " << expected[1] << endl;cout << " 实际: " << result[0] << ", " << result[1] << endl;}return passed;
}int main() {setConsoleUTF8();cout << "足球小组赛出线判定测试" << endl;cout << "======================" << endl;int totalTests = 0;int passedTests = 0;// 测试用例和期望结果vector<tuple<vector<vector<int>>, vector<string>, string>> testCases = {//测试用例1: 正常情况{{{2,1}, {1,1}, {3,0}, {2,2}, {1,0}, {2,1}}, {"A", "C"}, "正常情况 - A队优势明显"},// 测试用例2: 全平局(按字母顺序){{{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}}, {"A", "B"}, "全平局 - 按字母顺序排名"},// 测试用例3: 积分相同,比较净胜球{{{1,0}, {0,1}, {2,0}, {1,0}, {0,2}, {1,0}}, {"A", "C"}, "积分相同比较净胜球"},// 测试用例4: 积分净胜球相同,比较进球数{{{2,1}, {1,2}, {3,0}, {0,3}, {2,0}, {0,2}}, {"A", "C"}, "积分净胜球相同比较进球数"},// 测试用例5: 强队全胜{{{3,0}, {4,0}, {5,0}, {3,0}, {4,0}, {2,1}}, {"A", "B"}, "强队全胜"},// 测试用例6: 胜负交替{{{1,0}, {0,1}, {1,0}, {0,1}, {1,0}, {0,1}}, {"A", "C"}, "胜负交替"},// 测试用例7: 高比分比赛{{{5,4}, {3,3}, {2,2}, {4,3}, {1,0}, {3,2}}, {"B", "A"}, "高比分比赛"},// 测试用例8: 防守型比赛{{{1,0}, {0,0}, {0,0}, {1,0}, {0,0}, {0,0}}, {"A", "B"}, "防守型比赛"},// 测试用例9: 三队同分情况{{{1,0}, {0,1}, {2,0}, {1,0}, {0,2}, {0,1}}, {"A", "D"}, "三队同分"},// 测试用例10: 所有比赛1-0{{{1,0}, {1,0}, {1,0}, {1,0}, {1,0}, {1,0}}, {"A", "B"}, "所有比赛1-0"},// 测试用例11: D队全败{{{2,0}, {3,0}, {1,0}, {2,1}, {1,0}, {2,0}}, {"A", "B"}, "D队全败"},// 测试用例12: 复杂同分情况{{{2,2}, {1,1}, {0,0}, {2,2}, {1,1}, {0,0}}, {"B", "A"}, "复杂同分"},// 测试用例13: 极端大比分{{{10,0}, {8,0}, {6,0}, {5,1}, {4,0}, {3,2}}, {"A", "B"}, "极端大比分"},// 测试用例14: 进攻型比赛{{{3,3}, {2,2}, {4,4}, {1,1}, {3,2}, {2,3}}, {"B", "D"}, "进攻型比赛"},// 测试用例15: 字母顺序决定排名{{{1,1}, {1,1}, {1,1}, {1,1}, {1,1}, {1,1}}, {"A", "B"}, "完全相同的战绩按字母顺序"}};cout << endl;for (const auto& testCase : testCases) {totalTests++;if (runTest(get<0>(testCase), get<1>(testCase), get<2>(testCase))) {passedTests++;}cout << endl;}cout << "======================" << endl;cout << "测试结果: " << passedTests << "/" << totalTests << " 通过" << endl;if (passedTests == totalTests) {cout << "所有测试通过!" << endl;} else {cout << "有 " << (totalTests - passedTests) << " 个测试失败" << endl;}return 0;
}
