Olympic Game

Every time during the Olympic Games, we are very concerned about the situation medal tally rankings.

Now we assume that ranking rules of the medals table, in descending order of priority as follows:

  1. The number of medals more than the top surface;
  2. The number of silver and more standing in the front;
  3. The number of bronze and more standing in the front;
  4. If the above three conditions are still unable to distinguish rank, places lexicographic order of the country names.

We assume that the country name is not more than 20 characters, all kinds of medals not more than 100, and greater than or equal to 0.

Enter a description

A first line of input integer N (0 <N <21), represents the number of countries;

Then the next N rows, each row containing a string that represents the name of each country Namei, and three integers Gi, Si, Bi
denote the number of gold, silver, bronze obtained for each, separated by a space, such as (China 51 20 21).

Output Description

Sequential order output medals table, only the name of the exporting country, its own line.

Sample input

5
China 32 28 34
England 12 34 22
France 23 33 2
Japan 12 34 25
Rusia 23 43 0

Sample Output

China
Rusia
France
Japan
England

Code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct  country {
    char name[20];
    int gold;
    int silver;
    int bronze;
}Ctry;

// 交换array1与array2的值
void SwitchCtry(Ctry *array1, Ctry *array2)
{
    Ctry temp;
    
    if(!array1 || !array2)
    {
        return;
    } 
    temp = *array1;
    *array1 = *array2;
    *array2 = temp;
}

// 对array数组按照规则进行排序
void Sort(Ctry *array, int n)
{
    
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
            if(array[i].gold < array[j].gold)
            {
                SwitchCtry(&array[i], &array[j]);
            }
            else if (array[i].gold == array[j].gold)
            {
                if (array[i].silver < array[j].silver)
                {
                    SwitchCtry(&array[i], &array[j]);
                }
                else if (array[i].silver == array[j].silver)
                {
                    if (array[i].bronze < array[j].bronze)
                    {
                        SwitchCtry(&array[i], &array[j]);
                    }
                    else if (array[i].bronze == array[j].bronze)
                    {
                        //strcmp()函数用到<string.h>
                        if(strcmp(array[i].name, array[j].name) > 0)
                        {
                            SwitchCtry(&array[i], &array[j]);
                        }
                    }
                }
            }
        }
    }
}

int main()
{
    int n;
    Ctry *array = NULL;
    
    scanf("%d", &n);
    if (n <= 0 || n >= 21)
    {
        return 1;
    }
    // 开辟结构体指针空间
    array = (Ctry *)malloc(n * sizeof(Ctry));
    if(array == NULL)
    {
        return 1;
    }
    // 获取输入值
    for(int i = 0; i < n; i++)
    {
        scanf("%s %d %d %d", array[i].name, &array[i].gold, &array[i].silver, &array[i].bronze);
    }
    
    Sort(array, n);
    // 打印输出结果
    for (int j = 0; j < n; j++)
    {
        printf("%s\n", array[j].name);
    }
    // 释放指针
    free(array);
    // 指针释放后置NULL
    array = NULL;
    
    return 0;
}

Homepage:

www.codeapes.cn

Guess you like

Origin www.cnblogs.com/codeapes666/p/12093766.html