UVa 10194 Football (aka Soccer)
还不错,一次AC。读入数据时用cin/stringin比较方便处理,输出时则换用printf。另外注意多关键字排序,觉得这题还是使用多次排序比较方便编程。
以下是我的代码:
#include
<
iostream
>
#include < sstream >
#include < vector >
#include < string >
#include < algorithm >
#include < cstdio >
#include < cctype >
using namespace std;
struct Type
{
Type()
{
name_ = "" ;
score_ = win_ = lose_ = draw_ = goal_ = all_goal_ = game_ = 0 ;
}
string name_;
int score_,win_,lose_,draw_,goal_,all_goal_,game_;
};
int n;
vector < Type > r;
int Team( const string & team_name)
{
for ( int i = 0 ;i < n;i ++ )
if (r[i].name_ == team_name)
return i;
}
bool cmp_1( const Type & a, const Type & b)
{
string aa(a.name_),bb(b.name_);
for ( int i = 0 ;i < aa.size();i ++ )
aa[i] = tolower(aa[i]);
for ( int i = 0 ;i < bb.size();i ++ )
bb[i] = tolower(bb[i]);
return aa < bb;
}
bool cmp_2( const Type & a, const Type & b)
{
return a.game_ < b.game_;
}
bool cmp_3( const Type & a, const Type & b)
{
return a.all_goal_ > b.all_goal_;
}
bool cmp_4( const Type & a, const Type & b)
{
return a.goal_ > b.goal_;
}
bool cmp_5( const Type & a, const Type & b)
{
return a.win_ > b.win_;
}
bool cmp_6( const Type & a, const Type & b)
{
return a.score_ > b.score_;
}
int main()
{
int T;
cin >> T;
getchar();
bool first( true );
while (T -- )
{
string game_name;
getline(cin,game_name);
cin >> n;
getchar();
r.resize(n);
for ( int i = 0 ;i < n;i ++ )
getline(cin,r[i].name_);
int g;
cin >> g;
getchar();
string s;
for ( int i = 0 ;i < g;i ++ )
{
getline(cin,s);
int pos_1(s.find( ' # ' )),pos_2(s.rfind( ' # ' ));
string team_1(s.substr( 0 ,pos_1)),team_2(s.substr(pos_2 + 1 ));
istringstream sin(s.substr(pos_1 + 1 ));
int goal_1,goal_2;
sin >> goal_1;
char ch;
sin >> ch;
sin >> goal_2;
int team_1_num(Team(team_1)),team_2_num(Team(team_2));
r[team_1_num].all_goal_ += goal_1;
r[team_1_num].goal_ += (goal_1 - goal_2);
r[team_1_num].game_ ++ ;
r[team_2_num].all_goal_ += goal_2;
r[team_2_num].goal_ += (goal_2 - goal_1);
r[team_2_num].game_ ++ ;
if (goal_1 > goal_2)
{
r[team_1_num].score_ += 3 ;
r[team_1_num].win_ ++ ;
r[team_2_num].lose_ ++ ;
}
else if (goal_1 < goal_2)
{
r[team_2_num].score_ += 3 ;
r[team_2_num].win_ ++ ;
r[team_1_num].lose_ ++ ;
}
else
{
r[team_1_num].score_ ++ ;
r[team_2_num].score_ ++ ;
r[team_1_num].draw_ ++ ;
r[team_2_num].draw_ ++ ;
}
}
sort(r.begin(),r.end(),cmp_1);
stable_sort(r.begin(),r.end(),cmp_2);
stable_sort(r.begin(),r.end(),cmp_3);
stable_sort(r.begin(),r.end(),cmp_4);
stable_sort(r.begin(),r.end(),cmp_5);
stable_sort(r.begin(),r.end(),cmp_6);
if (first)
first = false ;
else
cout << endl;
cout << game_name << endl;
for ( int i = 0 ;i < n;i ++ )
printf( " %d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n " ,i + 1 ,r[i].name_.c_str(),r[i].score_,r[i].game_,r[i].win_,r[i].draw_,r[i].lose_,r[i].goal_,r[i].all_goal_,r[i].all_goal_ - r[i].goal_);
fill_n(r.begin(),n,Type());
}
return 0 ;
}
#include < sstream >
#include < vector >
#include < string >
#include < algorithm >
#include < cstdio >
#include < cctype >
using namespace std;
struct Type
{
Type()
{
name_ = "" ;
score_ = win_ = lose_ = draw_ = goal_ = all_goal_ = game_ = 0 ;
}
string name_;
int score_,win_,lose_,draw_,goal_,all_goal_,game_;
};
int n;
vector < Type > r;
int Team( const string & team_name)
{
for ( int i = 0 ;i < n;i ++ )
if (r[i].name_ == team_name)
return i;
}
bool cmp_1( const Type & a, const Type & b)
{
string aa(a.name_),bb(b.name_);
for ( int i = 0 ;i < aa.size();i ++ )
aa[i] = tolower(aa[i]);
for ( int i = 0 ;i < bb.size();i ++ )
bb[i] = tolower(bb[i]);
return aa < bb;
}
bool cmp_2( const Type & a, const Type & b)
{
return a.game_ < b.game_;
}
bool cmp_3( const Type & a, const Type & b)
{
return a.all_goal_ > b.all_goal_;
}
bool cmp_4( const Type & a, const Type & b)
{
return a.goal_ > b.goal_;
}
bool cmp_5( const Type & a, const Type & b)
{
return a.win_ > b.win_;
}
bool cmp_6( const Type & a, const Type & b)
{
return a.score_ > b.score_;
}
int main()
{
int T;
cin >> T;
getchar();
bool first( true );
while (T -- )
{
string game_name;
getline(cin,game_name);
cin >> n;
getchar();
r.resize(n);
for ( int i = 0 ;i < n;i ++ )
getline(cin,r[i].name_);
int g;
cin >> g;
getchar();
string s;
for ( int i = 0 ;i < g;i ++ )
{
getline(cin,s);
int pos_1(s.find( ' # ' )),pos_2(s.rfind( ' # ' ));
string team_1(s.substr( 0 ,pos_1)),team_2(s.substr(pos_2 + 1 ));
istringstream sin(s.substr(pos_1 + 1 ));
int goal_1,goal_2;
sin >> goal_1;
char ch;
sin >> ch;
sin >> goal_2;
int team_1_num(Team(team_1)),team_2_num(Team(team_2));
r[team_1_num].all_goal_ += goal_1;
r[team_1_num].goal_ += (goal_1 - goal_2);
r[team_1_num].game_ ++ ;
r[team_2_num].all_goal_ += goal_2;
r[team_2_num].goal_ += (goal_2 - goal_1);
r[team_2_num].game_ ++ ;
if (goal_1 > goal_2)
{
r[team_1_num].score_ += 3 ;
r[team_1_num].win_ ++ ;
r[team_2_num].lose_ ++ ;
}
else if (goal_1 < goal_2)
{
r[team_2_num].score_ += 3 ;
r[team_2_num].win_ ++ ;
r[team_1_num].lose_ ++ ;
}
else
{
r[team_1_num].score_ ++ ;
r[team_2_num].score_ ++ ;
r[team_1_num].draw_ ++ ;
r[team_2_num].draw_ ++ ;
}
}
sort(r.begin(),r.end(),cmp_1);
stable_sort(r.begin(),r.end(),cmp_2);
stable_sort(r.begin(),r.end(),cmp_3);
stable_sort(r.begin(),r.end(),cmp_4);
stable_sort(r.begin(),r.end(),cmp_5);
stable_sort(r.begin(),r.end(),cmp_6);
if (first)
first = false ;
else
cout << endl;
cout << game_name << endl;
for ( int i = 0 ;i < n;i ++ )
printf( " %d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n " ,i + 1 ,r[i].name_.c_str(),r[i].score_,r[i].game_,r[i].win_,r[i].draw_,r[i].lose_,r[i].goal_,r[i].all_goal_,r[i].all_goal_ - r[i].goal_);
fill_n(r.begin(),n,Type());
}
return 0 ;
}