Olympic Ranking

Topic description:
Every year the major media of the Olympic Games publish a ranking, but careful readers find that the rankings of different countries are slightly different. For example, when China ranks first in the total number of gold medals, the Chinese media announces the "Gold Medal List"; while the United States ranks first in the total number of medals, the US media announces the "Medical List". If a country with a small population publishes a "Medal List per National", maybe African countries will become the leader of the list... Now, please write a program to calculate it in the best way for each country you come to consult. Ranking.

Input format:
The first line of input gives two positive integers N and M (≤224, because there are 224 countries and regions in the world), which are the total number of countries and regions participating in the ranking, and those who came to consult The number of countries. For simplicity, we number the countries from 0 to N−1. After that, there are N lines of input, the i-th line gives the number of gold medals, medals, and national population (in millions) of the country numbered i−1. The numbers are all integers in the interval [0,1000], with spaces Separated. The last line gives the numbers of M countries that come to consult, separated by spaces.

Output format:
output the ranking of the countries that came to consult in one line: the calculation method number. Its ranking is calculated according to the most favorable method for the country; the calculation method is numbered: gold medal list = 1, medal list = 2, national gold medal list per capita = 3, national medal list per capita = 4. The output is separated by spaces, and there can be no extra spaces at the end of the output.

If a country has the same ranking in different ranking methods, the calculation method with the smallest number will be output.

Input example:
4 4
51 100 1000
36 110 300
6 14 32
5 18 40
0 1 2 3

Output example:
1:1 1:2 1:3 1:4

Sample diagram:
Insert picture description here
Problem analysis:
The key to solving the problem is to sort the keywords. Here I choose to call the library function qsort() directly to sort.
The detailed processing in this question includes:
a. Sorting by a certain keyword and then paralleling the ranking. A simple method is to scan the ordered sequence after sorting, use a temporary variable to record the current ranking, compare the current data with the previous data, if the value is equal, continue to use the current ranking, otherwise update the current ranking to the position of the current data in the sequence .
b. If a country has the same ranking in different ranking methods, the calculation method with the smallest number must be output. Two variables can be set for each country, one to record the current best ranking, and the other to record the keyword number corresponding to the ranking. Sort and sort the rankings starting from the keyword with the smallest number. Only when the best ranking of the country is updated, the corresponding keyword number (that is, the number of the calculation method) will be changed.

As usual, code first, and then let's analyze it slowly.

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

int n,m; 

typedef struct{
    
    
   int id,gold,medal,population,rank,way,R;
   /*编号、金牌数、奖牌数、人口数、排名、计算方式编号、
     R用来存储临时排名,与当前排名进行比较。如果临时排名比当前排名靠前,则临时排名替换当前排名。*/
   double ave_gold,ave_medal;//人均金牌数、人均奖牌数。
}Q,*q;

Q a[230];
int b[230];//存储最后一行给出的m个前来咨询的国家编号。

struct Node{
    
    
	int rank,way;//用来存储最后的结果。 
}w[230];

int cmp1(const void *a,const void *b)
{
    
    
	q pa=(q)a;
	q pb=(q)b;
	int num1=pa->gold;
	int num2=pb->gold;
    return num2-num1;
}

int cmp2(const void *a,const void *b)
{
    
    
	q pa=(q)a;
	q pb=(q)b;
	int num1=pa->medal;
	int num2=pb->medal;
	return num2-num1;
}

int cmp3(const void *a,const void *b)
{
    
    
	q pa=(q)a;
	q pb=(q)b;
	double num1=pa->ave_gold;
	double num2=pb->ave_gold;
	if(num1-num2<0)
	 return 1;
	else
	 return -1;
}

int cmp4(const void *a,const void *b)
{
    
    
	q pa=(q)a;
	q pb=(q)b;
	double num1=pa->ave_medal;
	double num2=pb->ave_medal;
	if(num1-num2>0)
	 return -1;
	else
	 return 1;
}

int main()
{
    
    
	scanf("%d%d",&n,&m);
	
	int i;
    for(i=0;i<n;i++)
    {
    
    
    	a[i].id=i;//初始化。
    	scanf("%d%d%d",&a[i].gold,&a[i].medal,&a[i].population);
    	a[i].ave_gold=(double)a[i].gold/a[i].population;
		a[i].ave_medal=(double)a[i].medal/a[i].population; 
	}
	
	qsort(a,n,sizeof(Q),cmp1);
	for(i=0;i<n;i++)
	{
    
    
		a[i].way=1;//排名方法初始化为第一种方式。
		if(i>=1&&a[i].gold==a[i-1].gold)//记录排名,处理并列名次情况。
		 a[i].rank=a[i-1].rank;
		else
		 a[i].rank=i+1;//记录排名名次。
	}
	
	qsort(a,n,sizeof(Q),cmp2);
	for(i=0;i<n;i++)
	{
    
    
		if(i>=1&&a[i].medal==a[i-1].medal)R成员记录临时排名,方便与上一种排名方法的名次进行比较。
		 a[i].R=a[i-1].R;
		else
		 a[i].R=i+1;
		 
		if(a[i].rank>a[i].R)//与上次排名相比看是否能做到更优
		{
    
                       //若当前比较方法与上一种比较方法更优,则排名与方法都更新,以下两种方式同理。
			a[i].rank=a[i].R;
			a[i].way=2;
		}
	}
	
	qsort(a,n,sizeof(Q),cmp3);
	for(i=0;i<n;i++)
	{
    
    
		if(i>=1&&a[i].ave_gold==a[i-1].ave_gold)
		 a[i].R=a[i-1].R;
		else
		 a[i].R=i+1;
		 
		if(a[i].rank>a[i].R)
		{
    
    
			a[i].rank=a[i].R;
			a[i].way=3;
		}
	}
	
	qsort(a,n,sizeof(Q),cmp4);
	for(i=0;i<n;i++)
	{
    
    
		if(i>=1&&a[i].ave_medal==a[i-1].ave_medal)
		 a[i].R=a[i-1].R;
		else
		 a[i].R=i+1;
		 
		if(a[i].rank>a[i].R)
		{
    
    
			a[i].rank=a[i].R;
		    a[i].way=4;
		}
	}
	
	for(i=0;i<n;i++)//w结构体数组记录每一个编号的答案。
	{
    
    
		w[a[i].id].rank=a[i].rank;
		w[a[i].id].way=a[i].way;
	}
	
	for(i=1;i<=m;i++)
	 scanf("%d",&b[i]);
	
	for(i=1;i<m;i++)
     printf("%d:%d ",w[b[i]].rank,w[b[i]].way);
    
    printf("%d:%d",w[b[m]].rank,w[b[m]].way);
	return 0;
 } 

Reference material portal:
https://blog.csdn.net/a2580ah/article/details/109101818?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&dist_request_id=844bf240-3fe9-depth -utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

This question mainly examines the call of the qsort() function, which has been improved to a certain extent on this basis, and there is a certain degree of difficulty.
Well, this sharing will stop here first. If you have any questions about this, please leave me a message in the comment area. Thank you all for joining us!

Guess you like

Origin blog.csdn.net/qq_46139801/article/details/113913391