Created
February 24, 2014 04:05
-
-
Save mohamed-ennahdi/9181784 to your computer and use it in GitHub Desktop.
Tournament Standings Contest Problem.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* | |
* Maximum number of tournaments we can have. | |
*/ | |
#define MAX 1000 - 1 | |
/* | |
* Maximum size of the description string. | |
*/ | |
#define DESC 80 + 1 | |
/* | |
* Maximum size of the team name string. | |
*/ | |
#define TEAMNAME 25 + 1 | |
/* | |
* Maximum number of teams we can have in a tournament. | |
*/ | |
#define TEAMNUMBER 16 | |
/* | |
* Maxim number of games we can have in a tournament. | |
*/ | |
#define GAMENUMBER 100 | |
/* | |
* Maximum size length for the score string. | |
*/ | |
#define GAMESIZE 100 + 1 | |
/* | |
* Participation structure gathers: | |
* - team name | |
* - points | |
* - wins | |
* - goals scored | |
* - goals against | |
* - number of games played. | |
*/ | |
typedef struct { | |
char teamName[TEAMNAME]; | |
unsigned short points; | |
unsigned short wins; | |
unsigned short goalsScored; | |
unsigned short goalsAgainst; | |
unsigned short gamesPlayed; | |
} participation_t; | |
/* | |
* Tournament structure gathers: | |
* - tournament description | |
* - number of teams | |
* - wins | |
* - 2D array: tournament's team names | |
* - number of games | |
* - 2D array: result of games (which team, number of goals scored, against whom?) | |
* - | |
*/ | |
typedef struct { | |
char desc[DESC]; | |
unsigned short numberOfTeams; | |
char teamsName[TEAMNUMBER][TEAMNAME]; | |
unsigned short numberOfGames; | |
char resultOfGames[GAMENUMBER][GAMESIZE]; | |
participation_t participations[TEAMNUMBER]; | |
} tournament_t; | |
/* | |
* Insertion sort (algortihm original algorithm taken from: | |
* http://www.personal.kent.edu/~rmuhamma/Algorithms/algorithm.html | |
*/ | |
void insertionSort(participation_t numbers[], const int array_size) { | |
int i, j; | |
participation_t index; | |
for (i = 1; i < array_size; i++) { | |
index = numbers[i]; | |
j = i; | |
while ((j > 0) && (numbers[j - 1].points < index.points || numbers[j - 1].wins < index.wins || (numbers[j - 1].goalsScored < index.goalsScored && numbers[j - 1].goalsAgainst > index.goalsAgainst) || numbers[j - 1].gamesPlayed < index.gamesPlayed )) { | |
numbers[j] = numbers[j - 1]; | |
j = j - 1; | |
} | |
numbers[j] = index; | |
} | |
} | |
int main() { | |
char str[] = ""; | |
char delims[] = "#@"; | |
char *result = NULL; | |
char team1[TEAMNAME], team2[TEAMNAME]; | |
tournament_t tournaments[MAX]; | |
unsigned short n, i, j, k, l, score1, score2; | |
/* | |
* Begin Input | |
*/ | |
scanf("%d", &n); | |
fflush(stdin); | |
for (i = 0; i < n; i++) { // O(n) where is the number of tournaments | |
gets(tournaments[i].desc); | |
fflush(stdin); | |
scanf("%d", &tournaments[i].numberOfTeams); | |
fflush(stdin); | |
/* | |
* Initializing participants data. | |
*/ | |
for (j = 0; j < tournaments[i].numberOfTeams; j++) { // O(t) where t is the number of teams | |
gets(tournaments[i].teamsName[j]); | |
strcpy(tournaments[i].participations[j].teamName, tournaments[i].teamsName[j]); // O(1) because the size of a team name is below 26 | |
tournaments[i].participations[j].points = 0; | |
tournaments[i].participations[j].wins = 0; | |
tournaments[i].participations[j].goalsScored = 0; | |
tournaments[i].participations[j].goalsAgainst = 0; | |
tournaments[i].participations[j].gamesPlayed = 0; | |
} | |
fflush(stdin); | |
scanf("%d", &tournaments[i].numberOfGames); | |
fflush(stdin); | |
/* | |
* Entering result of games with the follow format: | |
* <team1>#<score1>@<score2>#<team2> | |
* Where: | |
* <team1> is the name of the first team (identifier) | |
* <score1> is the score of the first team | |
* <score2> is the score of the second team | |
* <team2> is the name of the second team (identifier) | |
*/ | |
for (j = 0; j < tournaments[i].numberOfGames; j++) { // O(g) where g is the number of games | |
gets(tournaments[i].resultOfGames[j]); | |
fflush(stdin); | |
} | |
/* | |
* Tokenizing the result of games into 4 elements (thanks to the delimiters '#' and '@') | |
* This operation yields the four 4 elements involved in string. | |
* | |
*/ | |
for (j = 0; j < tournaments[i].numberOfGames; j++) { // O(g) where g is the number of games | |
result = (char*) strtok( tournaments[i].resultOfGames[j], delims ); | |
k = 0; | |
while( result != NULL ) { // O(1) This loops iterates four times | |
if (k == 0) { | |
/* | |
* Team1 name | |
*/ | |
strcpy(team1, result); | |
} else { | |
if (k == 1) { | |
/* | |
* Team1 score | |
*/ | |
score1 = atoi(result); | |
} else { | |
if (k == 2) { | |
/* | |
* Team2 score | |
*/ | |
score2 = atoi(result); | |
} else { | |
/* | |
* Team2 name | |
*/ | |
strcpy(team2, result); | |
} | |
} | |
} | |
result = (char*) strtok( NULL, delims ); | |
k++; | |
} | |
/* | |
* Assigning each tokenized element in its appropriate location. | |
* => participation structure. | |
* Tokenizing allows us to interpret values and perform operations accordingly. | |
*/ | |
for (k = 0; k < tournaments[i].numberOfGames; k++) { // O(g) where g is the number of games | |
/* | |
* Processing Team 1 (Win, tie or loss) | |
*/ | |
if (strcmp(tournaments[i].participations[k].teamName, team1) == 0) { | |
tournaments[i].participations[k].goalsScored += score1; | |
tournaments[i].participations[k].goalsAgainst += score2; | |
tournaments[i].participations[k].gamesPlayed++; | |
if (score1 > score2) { // Team 1 wins | |
tournaments[i].participations[k].points += 3; | |
tournaments[i].participations[k].wins++; | |
} else { | |
if (score1 == score2) { // Team 1 and Team 2 tie | |
tournaments[i].participations[k].points ++; | |
} | |
} | |
} | |
/* | |
* Processing Team 2 (Win, tie or loss) | |
*/ | |
if (strcmp(tournaments[i].participations[k].teamName, team2) == 0) { | |
tournaments[i].participations[k].goalsScored += score2; | |
tournaments[i].participations[k].goalsAgainst += score1; | |
tournaments[i].participations[k].gamesPlayed++; | |
if (score2 > score1) { // Team 2 wins | |
tournaments[i].participations[k].points += 3; | |
tournaments[i].participations[k].wins++; | |
} else { | |
if (score1 == score2) { // Team 1 and Team 2 tie | |
tournaments[i].participations[k].points ++; | |
} | |
} | |
} | |
} | |
} | |
} | |
/* | |
* End Input | |
*/ | |
/* | |
* Browsing teams, sorting the participating teams so that the can be ranked | |
* by their points, wins, goal difference, most goals scored, and fewest games played. | |
*/ | |
for (i = 0; i < n; i++) { // O(n) where n is the number of tournaments | |
printf("\n%s", tournaments[i].desc); | |
/* | |
* Sorting teams by tournament. | |
*/ | |
insertionSort(tournaments[i].participations, tournaments[i].numberOfTeams);//O(t²) where t is the number of teams | |
/* | |
* Displaying teams ranking by tournament. | |
*/ | |
for (k = 0; k < tournaments[i].numberOfTeams; k++) { // O(t) where t is the number of teams | |
printf("\n%-25s %d", tournaments[i].participations[k].teamName, tournaments[i].participations[k].points); | |
} | |
printf("\n"); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment