This solution gets "Wrong answer" result inhttp://www.programming-challenges.com/,
but it gets "Accepted" result inhttp://uva.onlinejudge.org/.
I don't know why so far.
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <stdlib.h>
using namespace std;
const int DELTA = 'a' - 'A';
class Team
{
public:
typedef Team* TeamPtr;
class Comparer
{
public:
bool operator()(const TeamPtr& a, const TeamPtr& b)
{
if (a->m_Score > b->m_Score) return true;
if (a->m_Score < b->m_Score) return false;
if (a->m_WonRounds > b->m_WonRounds) return true;
if (a->m_WonRounds < b->m_WonRounds) return false;
if (a->GetNetGoals() > b->GetNetGoals()) return true;
if (a->GetNetGoals() < b->GetNetGoals()) return false;
if (a->m_WonGoals > b->m_WonGoals) return true;
if (a->m_WonGoals < b->m_WonGoals) return false;
if (a->GetRounds() < b->GetRounds()) return true;
if (a->GetRounds() > b->GetRounds()) return false;
return (a->m_LowCaseName.compare(b->m_LowCaseName) < 0);
}
};
int GetRounds()
{
return m_WonRounds + m_SameRounds + m_DefeatedRounds;
}
int GetNetGoals()
{
return m_WonGoals - m_LostGoals;
}
Team(string name) : m_Name(name), m_Score(0), m_WonRounds(0), m_SameRounds(0),
m_DefeatedRounds(0), m_WonGoals(0), m_LostGoals(0)
{
m_LowCaseName = m_Name;
int len = m_LowCaseName.length();
for (int i = 0; i < len; ++i)
{
if ((m_LowCaseName[i] >= 'A') && (m_LowCaseName[i] <= 'Z'))
m_LowCaseName[i] += DELTA;
}
}
void Output()
{
cout << m_Name << ' ' << m_Score << "p, " << GetRounds() << "g ("
<< m_WonRounds << '-' << m_SameRounds << '-' << m_DefeatedRounds
<< "), " << GetNetGoals() << "gd (" << m_WonGoals << '-' << m_LostGoals << ')' << endl;
}
string m_Name;
string m_LowCaseName;
int m_Score;
int m_WonRounds;
int m_SameRounds;
int m_DefeatedRounds;
int m_WonGoals;
int m_LostGoals;
};
static void GetRoundInfoFromLine(char* line, vector<Team::TeamPtr>& teams, map<string, int>& involvedTeams)
{
int i = 0;
while(line[i] != '#')
++i;
line[i] = '\0';
string name = line;
Team& player1 = *(teams[involvedTeams[name]]);
line += i + 1;
i = 0;
while(line[i] != '@')
++i;
line[i] = '\0';
int player1Goal = atoi(line);
player1.m_WonGoals += player1Goal;
line += i + 1;
i = 0;
while(line[i] != '#')
++i;
line[i] = '\0';
int player2Goal = atoi(line);
line += i + 1;
name = line;
Team& player2 = *(teams[involvedTeams[name]]);
player2.m_WonGoals += player2Goal;
player1.m_LostGoals += player2Goal;
player2.m_LostGoals += player1Goal;
if (player1Goal > player2Goal)
{
++player1.m_WonRounds;
++player2.m_DefeatedRounds;
player1.m_Score += 3;
}
else if (player1Goal == player2Goal)
{
++player1.m_Score;
++player2.m_Score;
++player1.m_SameRounds;
++player2.m_SameRounds;
}
else
{
++player2.m_WonRounds;
++player1.m_DefeatedRounds;
player2.m_Score += 3;
}
}
static void RunTestCase()
{
string matchName;
getline(cin, matchName);
int teamsCnt;
cin >> teamsCnt;
cin.ignore(); // Consume the '\n' after the count of the teams.
vector<Team::TeamPtr> teams;
map<string, int> involvedTeams;
string line;
for (int i = 0; i < teamsCnt; ++i)
{
getline(cin, line);
teams.push_back(new Team(line));
involvedTeams.insert(pair<string, int>(line, teams.size() - 1));
}
int roundsCnt;
cin >> roundsCnt;
cin.ignore(); // Consume the '\n' after the count of the rounds.
for (int i = 0; i < roundsCnt; ++i)
{
getline(cin, line);
GetRoundInfoFromLine(const_cast<char*>(line.c_str()), teams, involvedTeams);
}
sort(teams.begin(), teams.end(), Team::Comparer());
cout << matchName << endl;
for (int i = 0; i < teamsCnt; ++i)
{
cout << i + 1 << ") ";
teams[i]->Output();
delete teams[i];
}
}
static void Test()
{
int cnt;
cin >> cnt;
cin.ignore(); // Consume the '\n' after the count of the test cases.
for (int i = 0; i < cnt; ++i)
{
if (i != 0)
cout << endl;
RunTestCase();
}
}
int main(int argc, char* argv[])
{
Test();
return 0;
}
本文介绍了一个比赛排名系统的实现细节,包括如何使用C++编程语言、STL容器和自定义比较器来处理球队的比赛成绩,并根据特定规则进行排序。通过解析输入数据、维护球队状态和输出最终排名,该系统能准确地反映出每支队伍的表现。
2755

被折叠的 条评论
为什么被折叠?



