题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=98&problem=1135&mosmsg=Submission+received+with+ID+9393629
#include<iostream>
#include<sstream>
#include<string>
#include<cctype>
#include<algorithm>
using namespace std;
struct Info
{
string name;
int points;
int game_num;
int win;
int tie;
int lose;
int goal_dif;
int goal_scored;
int goal_against;
Info():name(""),points(0),game_num(0),win(0),tie(0),lose(0),goal_dif(0),goal_scored(0),goal_against(0){}
};
bool cmp(const Info &a, const Info &b)
{
if(a.points != b.points) return a.points > b.points;
else if(a.win != b.win) return a.win > b.win;
else if(a.goal_dif != b.goal_dif) return a.goal_dif > b.goal_dif;
else if(a.goal_scored != b.goal_scored) return a.goal_scored > b.goal_scored;
else if(a.game_num != b.game_num) return a.game_num < b.game_num;
string s1(a.name), s2(b.name);
for(int i = 0; i < s1.size(); ++i) s1[i] = tolower(s1[i]);
for(int i = 0; i < s2.size(); ++i) s2[i] = tolower(s2[i]);
/**//*
* 以下是错误写法:(s1, s2尚未分配内存,不能用下面方式赋值)
* string s1, s2;
* for(int i = 0; i < a.name.size(); ++i) s1[i] = tolower(a.name.at(i));
* for(int i = 0; i < b.name.size(); ++i) s2[i] = tolower(b.name.at(i));
*/
return s1 < s2;
}
void proc(const string &s, Info *team, int T)
{
string::size_type l = s.find('#');
string::size_type r = s.rfind('#');
string t1 = s.substr(0, l);
string t2 = s.substr(r + 1);
stringstream str(s.substr(l + 1, r - l - 1));
int g1, g2;
char t;
str >> g1 >> t >> g2;
for(int i = 0; i < T; ++i)
{
if(team[i].name == t1)
{
++team[i].game_num;
team[i].goal_dif += g1 - g2;
team[i].goal_scored += g1;
team[i].goal_against += g2;
if(g1 > g2)
{
team[i].points += 3;
++team[i].win;
}
else if(g1 == g2)
{
team[i].points += 1;
++team[i].tie;
}
else
{
++team[i].lose;
}
}
if(team[i].name == t2)
{
++team[i].game_num;
team[i].goal_dif += g2 - g1;
team[i].goal_scored += g2;
team[i].goal_against += g1;
if(g2 > g1)
{
team[i].points += 3;
++team[i].win;
}
else if(g2 == g1)
{
team[i].points += 1;
++team[i].tie;
}
else
{
++team[i].lose;
}
}
}
}
int main()
{
int N;
cin >> N;
cin.ignore();
while(N--)
{
Info team[32];
int T, G;
string cup_name;
string s;
getline(cin, cup_name);
cin >> T;
cin.ignore();
for(int i = 0; i < T; ++i)
{
getline(cin, s);
team[i].name = s;
}
cin >> G;
cin.ignore();
for(int i = 0; i < G; ++i)
{
getline(cin, s);
proc(s, team, T);
}
sort(team, team + T, cmp);
cout << cup_name << endl;
for(int i = 0; i < T; ++i)
cout << i + 1 << ')' << ' ' << team[i].name << ' ' << team[i].points \
<< "p, " << team[i].game_num << "g (" << team[i].win << '-' << team[i].tie \
<< '-' << team[i].lose << "), " << team[i].goal_dif << "gd (" \
<< team[i].goal_scored << '-' << team[i].goal_against << ')' << endl;
if(N != 0) cout << endl;
}
return 0;
}
posted on 2011-10-21 15:27
wuxu 阅读(244)
评论(0) 编辑 收藏 引用 所属分类:
模拟