The main problem we're dealing with is finding a suitable data structure. First of all, it should allow updating teams' scores and total goals, with every passing match. Secondly, it should facilitate custom sorting, based on the given criteria (number of points > number of goals > team name). Most modern programming languages provide such a data structure: map + vector from C++'s Standard Template Library, or a dictionary in Python.
C++11 solution (Sergiu Puscas)
1 #include <iostream> 2 #include <map> 3 #include <vector> 4 #include <string> 5 #include <algorithm> 6 #define totalPoints first 7 #define totalGoals second 8 #define name first 9 #define stats second 10 using namespace std; 11 12 map <string, pair <int, int>> M; 13 vector <pair <string, pair <int, int>>> v; 14 int goalsA, goalsB, bonusA, bonusB; 15 string teamA, teamB; 16 17 bool cmp(pair <string, pair <int, int>> A, pair <string, pair <int, int>> B) { 18 if(A.stats.totalPoints != B.stats.totalPoints) 19 return A.stats.totalPoints > B.stats.totalPoints; 20 21 if(A.stats.totalGoals != B.stats.totalGoals) 22 return A.stats.totalGoals > B.stats.totalGoals; 23 24 return A.name < B.name; 25 } 26 27 int main() { 28 for(int i=1; i<=6; i++) { 29 cin>>teamA>>teamB>>goalsA>>goalsB; 30 31 if(goalsA > goalsB) bonusA = 3, bonusB = 0; 32 else if(goalsA == goalsB) bonusA = 1, bonusB = 1; 33 else bonusA = 0, bonusB = 3; 34 35 if(M.find(teamA) == M.end()) M[teamA] = make_pair(0, 0); 36 if(M.find(teamB) == M.end()) M[teamB] = make_pair(0, 0); 37 38 M[teamA] = make_pair(M[teamA].totalPoints + bonusA, M[teamA].totalGoals + goalsA); 39 M[teamB] = make_pair(M[teamB].totalPoints + bonusB, M[teamB].totalGoals + goalsB); 40 } 41 42 for(auto team:M) v.push_back(team); 43 sort(v.begin(), v.end(), cmp); 44 for(auto team:v) cout<<team.name<<"\n"; 45 46 return 0; 47 }
Python solution
1 scoreboard, goalboard = {}, {} 2 3 for _ in range(0, 6): 4 teamA, teamB, goalsA, goalsB = raw_input().split() 5 goalsA, goalsB = map(int, [goalsA, goalsB]) 6 7 if goalsA > goalsB: scoreA, scoreB = 3, 0 8 elif goalsA == goalsB: scoreA, scoreB = 1, 1 9 else: scoreA, scoreB = 0, 3 10 11 if not teamA in scoreboard: scoreboard[teamA], goalboard[teamA] = 0, 0 12 if not teamB in scoreboard: scoreboard[teamB], goalboard[teamB] = 0, 0 13 14 scoreboard[teamA] += scoreA 15 scoreboard[teamB] += scoreB 16 17 goalboard[teamA] += goalsA 18 goalboard[teamB] += goalsB 19 20 print '\n'.join(["%s" % x[2] for x in sorted([(scoreboard[x], goalboard[x], x) for x in scoreboard], key=lambda x: (-x[0], -x[1], x[2]))])