按时间顺序给出奥运会的获奖情况,根据获奖情况输出当前奥运金牌榜。 输入 第一行一个整数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;
}
posted @   xuchaoxin1375  阅读(14)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示