Algorithm
problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1639
Write a program, that given the fixtures of a football championship, outputs the corresponding classification following the format specified below. Win, draw and loss earn respectively three, one and zero points. The criteria of classification are the number of points scored, followed by goal difference (goals scored minus goals suffered), and then scored goals. When more than one team have exactly the same number of points, goal difference, and scored goals, these are considered as having the same position in the classification. Input The input will consist of a series of tests. Each test starts with a line containing two positive integers 28 ≥ T ≥ 1 and G ≥ 0. T is the number of teams and G is the number of games played. Follow then T lines, each containing the name of a squad. Squad names have up to 15 characters, and may only contain letters and dash characters (‘-’). Finally the following G lines contain the score of each game. The scores are output with the following format: name of home team, number of goals scored by home team, a dash, number of goals scored by away team, and name of away team. The input ends with a test case where T = G = 0 and should not be processed. Output The program shall output the classification tables corresponding to each input test separated by blank lines. In each table, the teams appear in order of classification, or alphabetically when they have the same position. The statistics of each team are displayed on a single line: team position, team name, number of points, number of games played, number of scored goals, number of suffered goals, goal difference, and percentage of earned points, when available. Note that if several teams have are in a draw, only the position of the first is printed. Fields shall be formatted and aligned as shown in the sample output.
Sample Input 6 10 tA tB tC td tE tF tA 1 - 1 tB tC 0 - 0 td tE 0 - 0 tA tC 0 - 0 tB td 0 - 0 tE tA 0 - 0 tC tB 0 - 0 tE td 0 - 0 tA tE 0 - 0 tC tB 0 - 0 td 2 2 Botafogo Flamengo Botafogo 3 - 2 Flamengo Flamengo 2 - 3 Botafogo 5 10 tA tB tC tD tE tA 0 - 0 tB tC 0 - 0 tD tE 0 - 0 tA tC 0 - 0 tB tD 0 - 0 tE tA 0 - 0 tC tB 0 - 0 tE tD 0 - 0 tA tE 0 - 0 tC tB 0 - 0 tD 3 2 Quinze-Novembro Flamengo Santo-Andre Quinze-Novembro 6 - 0 Flamengo Flamengo 0 - 2 Santo-Andre 0 0
Sample Output 1. tA 4 4 1 1 0 33.33 tB 4 4 1 1 0 33.33 3. tC 4 4 0 0 0 33.33 td 4 4 0 0 0 33.33 tE 4 4 0 0 0 33.33 6. tF 0 0 0 0 0 N/A 1. Botafogo 6 2 6 4 2 100.00 2. Flamengo 0 2 4 6 -2 0.00 1. tA 4 4 0 0 0 33.33 tB 4 4 0 0 0 33.33 tC 4 4 0 0 0 33.33 tD 4 4 0 0 0 33.33 tE 4 4 0 0 0 33.33 1. Quinze-Novembro 3 1 6 0 6 100.00 2. Santo-Andre 3 1 2 0 2 100.00 3. Flamengo 0 2 0 8 -8 0.00
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
using namespace std;
struct team {
string name;
int points, gplayed, scored, suffered,index;
team(string n,int i){
name = n;
index = i;
points = gplayed = scored = suffered = 0;
}
};
int main() {
int t,g;
string in;
string hteam, ateam;
int s1,s2;
bool notfirst = false;
while(scanf("%d %d",&t,&g),t){
if(notfirst) cout<<endl;
notfirst = true;
unordered_map < string,team*> teams;
for(int i=0;i i+It t;i++){
cin >> in;
team* n = new team(in,i);
teams[in] = n;
}
for(int i=0;i i+It g;i++){
cin >> hteam >> s1 >> in >> s2 >> ateam;
team* team1 = teams[hteam];
team* team2 = teams[ateam];
team1->gplayed ++; team2->gplayed++;
team1->scored += s1; team2->scored += s2;
team1->suffered += s2; team2->suffered += s1;
if(s1 == s2) {team1->points++, team2->points++;}
else if(s1 > s2) team1->points+=3;
else team2->points+=3;
}
vector < team*> res;
for(auto& p : teams) res.push_back(p.second);
sort(res.begin(), res.end(), [](team* a,team* b){
if(a->points != b->points) return a->points > b->points;
int gd1 = a->scored - a->suffered;
int gd2 = b->scored - b->suffered;
if(gd1 != gd2) return gd1 > gd2;
if(a->scored != b->scored) return a->scored > b->scored;
string nameA = a->name;
string nameB = b->name;
transform(nameA.begin(), nameA.end(), nameA.begin(), ::tolower);
transform(nameB.begin(), nameB.end(), nameB.begin(), ::tolower);
if(nameA == nameB) return a->index < b->index;
return nameA < nameB;
});
for(int i=0;i i+It res.size();i++){
int gd = res[i]->scored-res[i]->suffered;
double percentScore = (res[i]->points*100.0) / (res[i]->gplayed*3.0);
bool sameAsPrevPosition = (i != 0 &&
(res[i]->points == res[i-1]->points && res[i]->scored == res[i-1]->scored
&& gd == res[i-1]->scored-res[i-1]->suffered));
cout << setw(3) << (sameAsPrevPosition ? "" : to_string(i+1) + ".");
cout << setw(16) << res[i]->name << setw(4) << res[i]->points;
cout << setw(4) << res[i]->gplayed << setw(4) << res[i]->scored;
cout << setw(4) << res[i]->suffered << setw(4) << gd;
// property of Nan is that self comparison is always false
if(percentScore != percentScore) cout << setw(7) << "N/A" << endl;
else cout << setw(7) << setprecision(2) << fixed << percentScore << endl;
}
}
}
Copy The Code &
Try With Live Editor
Input
tA
tB
tC
td
tE
tF
tA 1 - 1 tB
tC 0 - 0 td
tE 0 - 0 tA
tC 0 - 0 tB
td 0 - 0 tE
tA 0 - 0 tC
tB 0 - 0 tE
td 0 - 0 tA
tE 0 - 0 tC
tB 0 - 0 td
2 2
Botafogo
Flamengo
Botafogo 3 - 2 Flamengo
Flamengo 2 - 3 Botafogo
5 10
tA
tB
tC
tD
tE
tA 0 - 0 tB
tC 0 - 0 tD
tE 0 - 0 tA
tC 0 - 0 tB
tD 0 - 0 tE
tA 0 - 0 tC
tB 0 - 0 tE
tD 0 - 0 tA
tE 0 - 0 tC
tB 0 - 0 tD
3 2
Quinze-Novembro
Flamengo
Santo-Andre
Quinze-Novembro 6 - 0 Flamengo
Flamengo 0 - 2 Santo-Andre
0 0
Output
3. tC 4 4 0 0 0 33.33 td 4 4 0 0 0 33.33 tE 4 4 0 0 0 33.33
6. tF 0 0 0 0 0 N/A
1. Botafogo 6 2 6 4 2 100.00
2. Flamengo 0 2 4 6 -2 0.00
1. tA 4 4 0 0 0 33.33 tB 4 4 0 0 0 33.33 tC 4 4 0 0 0 33.33 tD 4 4 0 0 0 33.33 tE 4 4 0 0 0 33.33
1. Quinze-Novembro 3 1 6 0 6 100.00
2. Santo-Andre 3 1 2 0 2 100.00
3. Flamengo 0 2 0 8 -8 0.00
Demonstration
UVA Online Judge solution - 10698-Football Sort - UVA Online Judge solution in C,C++,java