PC/UVa:110408/10194
排序的比较函数有些复杂,嵌套了好多层条件判断。看了别人的一个写法,可以依次判断每个排名项是否相同,不相同则return
两项的比较结果,相同再判断下一个。本质上还是多层嵌套,但是看起来方便一些。这就要借用大师Donald Knuth的一句名言了:Programs are meant to be read by humans and only incidentally for computers to execute.
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
class Team
{
public:
string strName;
int point;
int game;
int win;
int tie;
int loss;
int difference;
int scored;
int against;
Team(const string &strName) :strName(strName),
point(0), game(0), win(0), tie(0), loss(0),
difference(0), scored(0), against(0){}
};
bool operator<(const Team &t1, const Team &t2)
{
if (t1.point > t2.point) return true;
else if (t1.point == t2.point){
if (t1.win > t2.win) return true;
else if (t1.win == t2.win){
if (t1.difference > t2.difference) return true;
else if (t1.difference == t2.difference){
if (t1.scored > t2.scored) return true;
else if (t1.scored == t2.scored){
if (t1.game < t2.game) return true;
else if (t1.game == t2.game){
string strName1(t1.strName), strName2(t2.strName);
for (char &ch : strName1) ch = tolower(ch);
for (char &ch : strName2) ch = tolower(ch);
return strName1 < strName2;
}
}
}
}
}
return false;
}
int main()
{
int N = 0;
cin >> N;
cin.get();
for (int n = 0; n < N; n++)
{
string strTour, strTeam, strGame;
getline(cin, strTour);
int T = 0, G = 0;
cin >> T;
cin.get();
map<string, int> mTeam2ID;
vector<string> vID2Team;
vector<Team> vecTeam;
for (int t = 0; t < T; t++)
{
getline(cin, strTeam);
mTeam2ID[strTeam] = t;
vID2Team.push_back(strTeam);
Team team(strTeam);
vecTeam.push_back(team);
}
cin >> G;
cin.get();
string strL, strR;
string::size_type posAt, posSharp;
int iLScored, iRScored, iLID, iRID;
for (int g = 0; g < G; g++)
{
getline(cin, strGame);
posAt = strGame.find('@');
strL = strGame.substr(0, posAt);
strR = strGame.substr(posAt + 1);
//处理比分左边
posSharp = strL.find('#');
iLID = mTeam2ID[strL.substr(0, posSharp)];
iLScored = stol(strL.substr(posSharp + 1));
//处理比分右边
posSharp = strR.find('#');
iRID = mTeam2ID[strR.substr(posSharp + 1)];
iRScored = stol(strR.substr(0, posSharp));
//计算进球、失球、净胜球
vecTeam[iLID].scored += iLScored;
vecTeam[iLID].against += iRScored;
vecTeam[iLID].difference = vecTeam[iLID].scored - vecTeam[iLID].against;
vecTeam[iRID].scored += iRScored;
vecTeam[iRID].against += iLScored;
vecTeam[iRID].difference = vecTeam[iRID].scored - vecTeam[iRID].against;
//计算赢、输、平局和比赛总数目
if (iLScored > iRScored){
vecTeam[iLID].win++;
vecTeam[iLID].point += 3;
vecTeam[iRID].loss++;
}
else if (iLScored == iRScored){
vecTeam[iLID].tie++;
vecTeam[iLID].point += 1;
vecTeam[iRID].tie++;
vecTeam[iRID].point += 1;
}
else{
vecTeam[iLID].loss++;
vecTeam[iRID].win++;
vecTeam[iRID].point += 3;
}
vecTeam[iLID].game++;
vecTeam[iRID].game++;
}
sort(vecTeam.begin(), vecTeam.end());
cout << strTour << endl;
for (size_t i = 0; i < vecTeam.size(); i++)
{
cout << i + 1 << ") " << vecTeam[i].strName << ' ';
cout << vecTeam[i].point << "p, ";
cout << vecTeam[i].game << "g ";
cout << '(' << vecTeam[i].win << '-' << vecTeam[i].tie << '-' << vecTeam[i].loss << "), ";
cout << vecTeam[i].difference << "gd ";
cout << '(' << vecTeam[i].scored << '-' << vecTeam[i].against << ')' << endl;
}
if (n != N - 1) cout << endl;
}
return 0;
}
/*
2
World Cup 1998 - Group A
4
Brazil
Norway
Morocco
Scotland
6
Brazil#2@1#Scotland
Norway#2@2#Morocco
Scotland#1@1#Norway
Brazil#3@0#Morocco
Morocco#3@0#Scotland
Brazil#1@2#Norway
Some strange tournament
5
Team A
Team B
Team C
Team D
Team E
5
Team A#1@1#Team B
Team A#2@2#Team C
Team A#0@0#Team D
Team E#2@1#Team C
Team E#1@2#Team D
*/