Olympics Ratings


General Statement: Read a file containing the names and medal counts of several countries who participated in the Olympics. Rank them by the number of gold medals earned. Secondly, rank them by the total number of medal earned.

Input: The first line in the data set is an integer that represents the number of data collections that follow. Each data collection contains the 3-letter abbreviation of the country name followed by the number of gold, silver, and bronze medals – in that order.

Output: For output #1, output the words BY GOLD, then each line will list the place ranking followed by the country name. If there is a tie, the first country is listed beside the place, and the additional countries are listed in vertical alignment. When finished, the country column is to be left-justified.
For output #2, output the words BY TOTAL. Follow the directions of output #1 for listing the countries.
All letters are upper case.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: All letters are upper case.
All country names are abbreviated to 3 letters.
The maximum number of countries is 12.

Discussion: First place is awarded to the one with the largest count of medals, second place to the second largest count, and so on.
If there is a tie, all will receive the same ranking and they are to be listed alphabetically within that rank. The next ranking awarded will be the rank as if there were no ties and each country was numbered consecutively.

Sample Input:
7
NOR 17 5 6
SWE 16 5 9
GBR 4 6 9
FRA 10 6 6
ITA 10 5 4
GER 17 6 6
AUS 18 3 2

Sample Output:
BY GOLD
1 AUS
2 GER
NOR
4 SWE
5 FRA
ITA
7 GBR

BY TOTAL
1 SWE
2 GER
3 NOR
4 AUS
5 FRA
6 GBR
ITA

#include <stdio.h>
#include <algorithm>
using namespace std;

struct country {
    char name [5];
    int gold;
    int silver;
    int bronze;
    int total;
} a [13];

int main ()
{
    int n;
    scanf ("%d", &n);

    for ( int i = 0; i < n; i++ ) {
        scanf ("%s", a [i].name);
        scanf ("%d", &a [i].gold);
        scanf ("%d", &a [i].silver);
        scanf ("%d", &a [i].bronze);
        a [i].total = a [i].gold + a [i].silver + a [i].bronze;
    }

    // descending sort by number of gold
    for ( int i = 0; i < n; i++ ) {
        for ( int j = i + 1; j < n; j++ ) {
            if ( a [i].gold < a [j].gold )
                swap (a [i], a [j]);
            else if ( a [i].gold == a [j].gold &&
            strcmp (a [i].name, a [j].name) > 0 )
                swap (a [i], a [j]);
        }
    }

    // output by gold
    printf ("BY GOLD\n");

    int i = 0;
    int tie = 0;

    do {
        if ( tie == a [i].gold )
            printf ("  %s\n", a [i].name);

        else {
            printf ("%d %s\n", i + 1, a [i].name);
            tie = a [i].gold;
        }
        i++;
    } while ( i < n );

    // descending sort by number of total
    for ( int i = 0; i < n; i++ ) {
        for ( int j = i + 1; j < n; j++ ) {
            if ( a [i].total < a [j].total )
                swap (a [i], a [j]);
            else if ( a [i].total == a [j].total &&
            strcmp (a [i].name, a [j].name) > 0 )
                swap (a [i], a [j]);
        }
    }

    // output by total
    printf ("\nBY TOTAL\n");

    i = 0;
    tie = 0;

    do {
        if ( tie == a [i].total )
            printf ("  %s\n", a [i].name);

        else {
            printf ("%d %s\n", i + 1, a [i].name);
            tie = a [i].total;
        }
        i++;
    } while ( i < n );

    return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s