按时间顺序给出奥运会的获奖情况,根据获奖情况输出当前奥运金牌榜。 输入 第一行一个整数N,代表奖牌的数量。

本文借鉴了:https://zhidao.baidu.com/question/1755296292799028948.html
按时间顺序给出奥运会的获奖情况,根据获奖情况输出当前奥运金牌榜。

输入
第一行一个整数N,代表奖牌的数量。
接下来N行,每行代表 (奖牌机器所属的国家的名字)和奖牌的类型。
为了简化问题,国家名中不存在空格。
N<=500
输出
按金牌数量降序输出每个国家的名称,及金银铜牌的数量,用空格隔开。
若金牌数相同,则比较银牌的数量。
若金牌银牌的数量都相同,则比较铜牌的数量。

样例输入
10
zhongguo jin
zhongguo tong
meiguo jin
yidali jin
faguo jin
faguo tong
meiguo yin
meiguo yin
eluosi tong
zhongguo jin
样例输出
zhongguo 2 0 1
meiguo 1 2 0
faguo 1 0 1
yidali 1 0 0
eluosi 0 0 1

#include <stdio.h>
#include <string.h>
#define GOLD 10000
#define SILVER 100
#define COPPER 1
void sort_link(int *weights,int* sort,int cnt)
{

    for (int i = 0; i < cnt; i++) //控制扫描次数
    {
        int max_index = 0;

        for (int j = 0; j < cnt; j++) //每次都从头到尾扫描
        {
            if (weights[max_index] < weights[j])
            {
                max_index = j;
            }
        }

        sort[i] = max_index;//保存该次扫描中的max_index;
        weights[max_index]= -1;//以此实现迭代
    }
}
int main()/* 如果int main 写作intmain,会遇到奇怪的undefined reference to `WinMain@16 */
{
    int count, i, j;
    int cnt = 0;

    char merged_countries[500][50];//countrie's names(after_merged)//no more than 500 items(medals).
    char orignal_countries[500];//save the countries's name(ororignal_countriesnal)
    char orignal_medals_data[500];// the kinds of metals (names of medals)

    int merged_results[500][4];//保存同一个国家累计的奖牌数据.
    int sort[500];
    int weights[500];
    scanf("%d", &count);
    for (i = 0; i < count; i++)
    {
        /* 读入数据(原始) */
        scanf("%s%s", orignal_countries, orignal_medals_data);
        /* after read one line of data,analyze at once */

        for (j = 0; merged_countries[j][0] != '\0'; j++)/* merged_countries[j][0] != '\0' :judge whether meet with the symbal of end_input. 
        the  merged_countries[j] stands for the countries' name,and merged_countries[j][0] indicates the first character of that line.
        we can take a test after a moment  */
        {
            if (  ! strcmp(merged_countries[j], orignal_countries)  )
            {
                /* do some match. */
                /* add the medal to the same merged_countries[j] countries*/
                if (!strcmp(orignal_medals_data, "jin"))
                    merged_results[j][0] += 1;
                else if (!strcmp(orignal_medals_data, "yin"))
                    merged_results[j][1] += 1;
                else if (!strcmp(orignal_medals_data, "tong"))
                    merged_results[j][2] += 1;

                break;//direct to analyze the next item.
            }
            //if merged_countries[j] is not (countries' name)  match with the item countries' name,than test the next merged_countries[j+]
        }
        //out of the last for_loop,and have test of the aready exiting names:
        if (merged_countries[j][0] == '\0')
        {
            cnt++;//每次新增国家条目是++;
            strcpy(merged_countries[j], orignal_countries);/* add the new name of the country into the merged_countries[j][] */
                /* do the match as well(like upper do) */
            if (!strcmp(orignal_medals_data, "jin"))
                merged_results[j][0] += 1;
            else if (!strcmp(orignal_medals_data, "yin"))
                merged_results[j][1] += 1;
            else if (!strcmp(orignal_medals_data, "tong"))
                merged_results[j][2] += 1;

        }
    }

    for(int i = 0;i<cnt;i++)
    {
        weights[i] = ( GOLD * merged_results[i][0] + SILVER * merged_results[i][1]+ 
        COPPER * merged_results[i][2] );
    }
    sort_link(weights,sort,cnt);
    for (int i,k = 0; k < cnt; k++)
    {
        i = sort[k];
        printf("%s %d %d %d\n", merged_countries[i], merged_results[i][0], 
        merged_results[i][1], merged_results[i][2]);
    }
        
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值