Data structure and algorithm topic set (Chinese) 7-40 Olympic ranking (25 points) (sort function)

1. topic

Every year, the major media of the Olympic Games will publish a ranking, but careful readers find that the ranking of different countries is slightly different. For example, when China's total number of gold medals ranks first, the Chinese media will release the "gold medal list"; while the United States' total number of medals ranks first, so the American media will release the "medal list". If a country with a small population publishes a "medal list of national per capita", maybe African countries will become the top Now please write a program to calculate the ranking of each consulting country in the most favorable way.

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 the number of countries coming for consultation. For simplicity, we number countries from 0 to n − 1. Then there are n lines of input. Line i shows the number of gold medals, medals and national population (in millions) of countries with i − 1. The numbers are integers in the [01000] range, separated by spaces. The last line gives the number of M countries to consult, separated by spaces.

Output format:

In a row, output the ranking of countries to consult in order: calculation method number. The ranking is calculated according to the most favorable way for the country; the number of calculation method is: gold medal table = 1, medal table = 2, gold medal table per capita = 3, National Medal table per capita = 4. The output is separated by spaces. No extra spaces are allowed at the end of the output.

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

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

2. Topic analysis

Note: when the quantity is the same, the ranking should also be tied!!

3. code

/*
Note that when the number is the same, the ranking should be the same!!!!
*/
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
using namespace std;
struct node
{
	int number;//number
	int	gold;//Number of gold medals
	int medal;//Number of medals
	double averagegold;//Gold medals per capita
	double averagemedal;//National medals per capita
	int goldnum;//Gold medal ranking
	int medalnum;//Medal ranking
	int averagegoldnum;//National gold medal ranking per capita
	int averagemedalnum;//Ranking of national per capita Awards
	int smallest;//Ranking minimum
	int smallesttype;//Minimum value type
}list[250];
bool cmpgold(node &a, node &b)
{
		return a.gold > b.gold;
}
bool cmpmedal(node &a, node &b)
{
	return a.medal > b.medal;
}
bool cmpaveragegold(node &a, node &b)
{
		return a.averagegold > b.averagegold;
}
bool cmpaveragemedal(node &a, node &b)
{
	return a.averagemedal > b.averagemedal;
}
bool cmpnumber(node &a, node &b)
{
	return a.number < b.number;
}

int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		list[i].number = i;
		list[i].gold = a;
		list[i].medal = b;
		list[i].averagegold =list[i].gold*1.0/c;
		list[i].averagemedal = list[i].medal*1.0/c;

	}
	int search[250];
	for (int i = 0; i < m; i++)
	{
		cin >> search[i];
	}
	sort(list, list + n, cmpgold);
	list[0].goldnum = 0;
	for (int i = 1; i < n; i++)
	{
		if (list[i - 1].gold != list[i].gold)

			list[i].goldnum = i;
		else
			list[i].goldnum = list[i-1].goldnum;//Note that when the quantity is the same, the ranking should be the same!!!!
	}
	sort(list, list + n, cmpmedal);
	list[0].medalnum = 0;
	for (int i = 1; i < n; i++)
	{
		if (list[i].medal != list[i-1].medal)
			list[i].medalnum = i;
		else
			list[i].medalnum = list[i - 1].medalnum;
	}
	sort(list, list + n, cmpaveragegold);
	list[0].averagegoldnum = 0;
	for (int i = 1; i < n; i++)
	{
		if (list[i].averagegold != list[i - 1].averagegold)
			list[i].averagegoldnum = i;
		else
			list[i].averagegoldnum = list[i - 1].averagegoldnum;
	}
	sort(list, list + n, cmpaveragemedal);
	list[0].averagemedalnum = 0;
	for (int i = 1; i < n; i++)
	{
		if (list[i].averagemedal != list[i - 1].averagemedal)
			list[i].averagemedalnum = i;
		else
			list[i].averagemedalnum = list[i - 1].averagemedalnum;
	}
    //Here are four rankings
	for (int i = 0; i < n; i++)
	{
		int min;
		int mini;
		if (list[i].goldnum >list[i].medalnum)
		{
			min = list[i].medalnum;
			mini = 2;
		}
		else
		{
			min = list[i].goldnum;
			mini = 1;
		}
		//***************************
		int min2;
		int mini2;
		if (list[i].averagegoldnum>list[i].averagemedalnum)
		{
			min2 = list[i].averagemedalnum;
			mini2 = 4;
		}
		else
		{
			min2 = list[i].averagegoldnum;
			mini2 = 3;
		}
		//**************************************
		int minf;
		int minif;
		if (min>min2)
		{
			minf = min2;
			minif =mini2 ;
		}
		else
		{
			minf = min;
			minif = mini;
		}

		list[i].smallest = minf;
		list[i].smallesttype = minif;
	}
    //Above is the minimum value and type
	int count = 0;
	sort(list, list + n, cmpnumber);//According to number
	for (int i = 0; i < m; i++)
	{
		if (count == 0)
		{
			cout << list[search[i]].smallest+1 << ":" << list[search[i]].smallesttype;
			count++;
		}
		else
		{
			cout <<" "<< list[search[i]].smallest+1 << ":" << list[search[i]].smallesttype;

		}
		
	}


}

 

Published 51 original articles, won praise 1, visited 455
Private letter follow

Posted on Mon, 10 Feb 2020 00:25:53 -0500 by Grizzzzzzzzzz