AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving ::Sorting/Searching
Description
模拟足球小组赛的规则, 通过比赛记录, 计算出最后的 rank 及队伍的各项数据。
rank 规则(优先级从高到低):
Type
Sorting/Searching
Analysis
入门级的恶心模拟题。
注意队名是含有大小写字母的, 而字典序中大小写没有区别。
这就造成了字典序, 和 STL string 默认的小于号, 有细微的差别。
这细微的差别可能就是你无限 WA 的根源。
Solution
// UVaOJ 10194
// Football(aka Soccer)
// by A Code Rabbit
#include
#include
#include
#include
#include
using namespace std;
string ToLower(string);
struct Team {
string name;
int point;
int game;
int win, tie, loss;
int scored, against, difference;
void Init() {
point = game = win = tie = loss = scored = against = difference = 0;
}
bool operator<(Team another) const {
if (this->point != another.point)
return this->point > another.point;
else if (this->win != another.win)
return this->win > another.win;
else if (this->difference != another.difference)
return this->difference > another.difference;
else if (this->scored != another.scored)
return this->scored > another.scored;
else if (this->game != another.game)
return this->game < another.game;
else
return ToLower(this->name) < ToLower(another.name);
}
};
string tournament;
int t;
Team team[32];
int g;
string team_name_1, team_name_2;
int goals1, goals2;
void Analyze(string team1, string team2, int goal1, int goal2);
int main() {
int tot_case;
cin >> tot_case;
cin.get();
while (tot_case--) {
// Input.
getline(cin, tournament);
cin >> t;
cin.get();
for (int i = 0; i < t; i++) {
team[i].Init();
getline(cin, team[i].name);
}
cin >> g;
cin.get();
// Input data and analyze data.
for (int i = 0; i < g; i++) {
getline(cin, team_name_1, '#');
cin >> goals1;
cin.get();
cin >> goals2;
cin.get();
getline(cin, team_name_2);
team_name_1 = str1;
team_name_2 = str2;
Analyze(team_name_1, team_name_2, goals1, goals2);
}
// Rank.
sort(team, team + t);
// Output.
printf("%s\n", tournament.c_str());
for (int i = 0; i < t; i++) {
printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",
i + 1, team[i].name.c_str(), team[i].point, team[i].game,
team[i].win, team[i].tie, team[i].loss, team[i].difference,
team[i].scored, team[i].against);
}
printf("%s", tot_case ? "\n" : "");
}
return 0;
}
string ToLower(string str) {
for (int i = 0; i < str.length(); i++)
str[i] = tolower(str[i]);
return str;
}
void Analyze(string team_name_1, string team_name_2, int goal1, int goal2) {
Team* team1;
Team* team2;
for (int i = 0; i < t; i++) {
if (team[i].name == team_name_1)
team1 = &team[i];
if (team[i].name == team_name_2)
team2 = &team[i];
}
if (goal1 > goal2) {
team1->point += 3;
team1->win++;
team2->loss++;
} else
if (goal1 < goal2) {
team1->loss++;
team2->win++;
team2->point += 3;
} else
if (goal1 == goal2) {
team1->point++;
team1->tie++;
team2->point++;
team2->tie++;
}
/* team1 */
team1->game++;
team1->scored += goal1;
team1->against += goal2;
team1->difference = team1->scored - team1->against;
/* team2 */
team2->game++;
team2->scored += goal2;
team2->against += goal1;
team2->difference = team2->scored - team2->against;
}