注解
1、结构体排序。要存储的是队名,积分,净胜球数,进球数。
2、用map记录球队数,判断每次读入的队名是否是新增的队名。
代码
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
struct Team {
string name;
int score;
int winGoal;
int goal;
};
int compare(Team t1, Team t2) {
if(t1.score!=t2.score) {
return t1.score>t2.score;
}
if(t1.winGoal!=t2.winGoal) {
return t1.winGoal>t2.winGoal;
}
if(t1.goal!=t2.goal) {
return t1.goal>t2.goal;
}
return t1.name<t2.name;
}
int main() {
int N;
while(cin>>N) {
Team t[N+1];
for(int i=1; i<=N; i++) {
t[i].name = "";
t[i].score = 0;
t[i].winGoal = 0;
t[i].goal = 0;
}
map<string, int> m;
int id = 0;
for(int i=0; i<N*(N-1); i++) {
string name1;
string vs;
string name2;
int goal1;
char c;
int goal2;
cin>>name1>>vs>>name2>>goal1>>c>>goal2;
int pos1 = m[name1];
if(pos1==0){
m[name1] = ++id;
t[id].name = name1;
pos1 = id;
}
int pos2 = m[name2];
if(pos2==0){
m[name2] = ++id;
t[id].name = name2;
pos2 = id;
}
if(goal1>goal2) {
t[pos1].score += 3;
} else if(goal1==goal2) {
t[pos1].score += 1;
t[pos2].score += 1;
} else {
t[pos2].score += 3;
}
t[pos1].winGoal += goal1-goal2;
t[pos2].winGoal += goal2-goal1;
t[pos1].goal += goal1;
t[pos2].goal += goal2;
}
sort(t+1, t+1+N, compare);
for(int i=1; i<=N; i++) {
cout<<t[i].name<<" "<<t[i].score<<endl;
}
cout<<endl;
}
return 0;
}