参考了别人的写法,挺不错的
http://www.cppblog.com/rakerichard/archive/2011/04/11/143931.aspx
注意要点:
1.算法 sort 和 stable_sort 的运用, 以及仿函数的编写
2.cin 与 getline混用的时候小心
3.string类成员函数的使用比如substr等,刚开始没考虑到比分可能不止一位数,WA了一次,还是用substr比较好
4.输出结果时候用printf比较好,当然注意%s与string类对象的关系
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <map>
using namespace std;
///
#define INF 0xffffff7
#define MAXN 200
///
struct rank{
rank()
{
name = "";
points = wins = ties = loses = goalDifference = goals_scored = goals_against = games = 0;
}
string name;
int points, wins, ties, loses, goalDifference, goals_scored, goals_against, games;
};
int N;
vector<rank> rankk;
//
bool cmp_1(const rank &a,const rank &b)
{
int i;
string aa(a.name),bb(b.name);
for(i=0;i<aa.size();i++)
aa[i]=tolower(aa[i]);
for(i=0;i<bb.size();i++)
bb[i]=tolower(bb[i]);
return aa < bb;
}
bool cmp_2(const rank &a,const rank &b)
{
return a.games < b.games;
}
bool cmp_3(const rank &a,const rank &b)
{
return a.goals_scored > b.goals_scored;
}
bool cmp_4(const rank &a,const rank &b)
{
return a.goalDifference > b.goalDifference;
}
bool cmp_5(const rank &a,const rank &b)
{
return a.wins > b.wins;
}
bool cmp_6(const rank &a,const rank &b)
{
return a.points > b.points;
}
int main()
{
///
int i, j;
cin >> N;
cin.ignore();
bool first = true;
while (N--)
{
rankk.clear();
string tournament;
getline(cin, tournament);
int teams;
cin >> teams;
cin.ignore();
rankk.resize(teams);
for (i = 0; i < teams; i++)
{
string teamname;
getline(cin, teamname);
rankk[i].name = teamname;
}
int matches;
cin >> matches;
cin.ignore();
for (i = 0; i < matches; i++)
{
int score1(0), score2(0);
string line;
getline(cin, line);
int pos_1(line.find('#')), pos_2(line.rfind('#'));
string Team1(line.substr(0, pos_1)), Team2(line.substr(pos_2 + 1));
istringstream sin(line.substr(pos_1 + 1));
int goal_1,goal_2;
sin>> score1;
char ch;
sin>> ch;
sin>> score2;
vector<rank>::iterator it = rankk.begin();
while (it != rankk.end())
{
if ( (*it).name == Team1 )
{
(*it).games++;
(*it).goals_scored += score1;
(*it).goals_against += score2;
(*it).goalDifference = (*it).goals_scored - (*it).goals_against;
if (score1 > score2)
{
(*it).points += 3;
(*it).wins++;
}
else if (score1 == score2)
{
(*it).points += 1;
(*it).ties++;
}
else
{
(*it).loses++;
}
}
if ( (*it).name == Team2)
{
(*it).games++;
(*it).goals_scored += score2;
(*it).goals_against += score1;
(*it).goalDifference = (*it).goals_scored - (*it).goals_against;
if (score2 > score1)
{
(*it).points += 3;
(*it).wins++;
}
else if (score2 == score1)
{
(*it).points += 1;
(*it).ties++;
}
else
{
(*it).loses++;
}
}
it++;
}
}
if (first)
first = false;
else
cout << endl;
sort(rankk.begin(), rankk.end(), cmp_1);
stable_sort(rankk.begin(), rankk.end(), cmp_2);
stable_sort(rankk.begin(), rankk.end(), cmp_3);
stable_sort(rankk.begin(), rankk.end(), cmp_4);
stable_sort(rankk.begin(), rankk.end(), cmp_5);
stable_sort(rankk.begin(), rankk.end(), cmp_6);
cout << tournament << endl;
for(i = 0; i < teams; i++)
printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", i+1, rankk[i].name.c_str(), rankk[i].points, rankk[i].games, rankk[i].wins, rankk[i].ties, rankk[i].loses, rankk[i].goalDifference, rankk[i].goals_scored, rankk[i].goals_scored - rankk[i].goalDifference);
}
///
return 0;
}