结构体排序(ACM俱乐部题号:1812)

该博客主要探讨ACM俱乐部题号1812的问题,涉及根据国家的奥运金牌、奖牌和人口对国家进行不同方式的排序。输入包含国家数、排名数及各国的奥运成绩,输出包括四种排名方式及其结果。文章提供了代码实现,并针对测试数据in2.txt进行了解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

输入:

输入包含有多组测试数据

第一行给我国家数n和要求排名的国家数m

第二行开始的n行,每行输入一个国家的奥运金牌数,奖牌数,人口数。每个国家都有一个编号,按照输入的先后顺序国家编号从0到n-1

接下来一行给出m个国家编号,按照升序给出,你需要对这m个国家进行排名

输出:

排名有4种方式:按金牌总数排名、按奖牌总数排名、按金牌人口比例排名、按奖牌人口比例排名。这4种排名方式的序号依次为1,2,3,4,且都是降序排列。

对每个需要进行排名的国家给出最佳排名方式和最终排名,格式:排名:排名方式

如果一个国家按照不同的排名方式却有相同的最终排名,则输出排名方式序号最小的那种排名

如果有多个国家并列排名的情况出现,例如4个国家的金牌数总数为100,90,90,80,则这4个国家按照金牌总数排序的排名为1,2,3,4(注意不是1,2,2,3)

最终的结果按照参与排名的国家的编号升序输出,并且每组输出数据的最后输出一个空行

代码如下:

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

typedef struct{
	int num;
	double orderValue[4];
	int order[4];
}nation;
nation na[100];

int cmp_0(const void *pa,const void *pb){
	return (*(nation*)pb).orderValue[0]>(*(nation*)pa).orderValue[0]?1:-1;
}
int cmp_1(const void *pa,const void *pb){
	return (*(nation*)pb).orderValue[1]>(*(nation*)pa).orderValue[1]?1:-1;
}
int cmp_2(const void *pa,const void *pb){
	return (*(nation*)pb).orderValue[2]>(*(nation*)pa).orderValue[2]?1:-1;
}
int cmp_3(const void *pa,const void *pb){
	return (*(nation*)pb).orderValue[3]>(*(nation*)pa).orderValue[3]?1:-1;
}
int cmp_4(const void *pa,const void *pb){
	return (*(nation*)pb).num<(*(nation*)pa).num?1:-1;
}

int main(){
	int n,m;
	int gold,total,pop;
	int need,i;
	int j,index;
	freopen("in2.txt","r",stdin);
	while(scanf("%d%d",&n,&m)!=EOF){
		for(i=0;i<n;i++){
			//input nation
			scanf("%d%d%d",&gold,&total,&pop);
			na[i].num=i;
			na[i].orderValue[0]=gold;
			na[i].orderValue[1]=total;
			na[i].orderValue[2]=1.0*gold/pop;
			na[i].orderValue[3]=1.0*total/pop;
		}
		for(i=0;i<m;i++){
			scanf("%d",&need);
			na[i]=na[need];
		}
		//order by gold
		qsort(na,m,sizeof(na[0]),cmp_0);
		for(i=0;i<m;i++){
			if(i>0&&na[i-1].orderValue[0]==na[i].orderValue[0])
				na[i].order[0]=na[i-1].order[0];
			else
				na[i].order[0]=i+1;
		}
		//order by total
		qsort(na,m,sizeof(na[0]),cmp_1);
		for(i=0;i<m;i++){
			if(i>0&&na[i-1].orderValue[1]==na[i].orderValue[1])
				na[i].order[1]=na[i-1].order[1];
			else
				na[i].order[1]=i+1;
		}
		//order by gold/pop
		qsort(na,m,sizeof(na[0]),cmp_2);
		for(i=0;i<m;i++){
			if(i>0&&na[i-1].orderValue[2]==na[i].orderValue[2])
				na[i].order[2]=na[i-1].order[2];
			else
				na[i].order[2]=i+1;
		}
		//order by total/pop
		qsort(na,m,sizeof(na[0]),cmp_3);
		for(i=0;i<m;i++){
			if(i>0&&na[i-1].orderValue[3]==na[i].orderValue[3])
				na[i].order[3]=na[i-1].order[3];
			else
				na[i].order[3]=i+1;
		}
		//output the information
		qsort(na,m,sizeof(na[0]),cmp_4);
		for(i=0;i<m;i++){
			index=0;
			//obtain th best
			for(j=0;j<4;j++){
				if(na[i].order[j]<na[i].order[index])
					index=j;
			}
			printf("%d:%d\n",na[i].order[index],index+1);
		}
		putchar('\n');
	}
	fclose(stdin);
	return 0;
}

in2.txt中测试数据为:

4 4
4 8 1
6 6 2
4 8 2
2 12 4
0 1 2 3
4 2
8 10 1
8 11 2
8 12 3
8 13 4
0 3



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值