Solution: Olympic scheduling problem (analog)

Subject description
as required, to the country to rank.
Enter
multiple sets of data.

The first line gives the number of countries N, the number of required state rank M, the national number from 0 to N-1.

The number of Olympic gold medal N lines starting with the second line of a given country or region, medals, Population (millions).

The next line gives the number M countries.
Output
Sort 4 ways: The total number of gold medals gold medal total population proportion of the population medals

Ranking gives the best way and the final ranking for each country

The format is: Ranking: way

If you have the same final ranking, the minimum output are ranked kind of ranking, a ranking for the total number of gold medals <medals <Gold population <population medals

If there are tied for the case, that if there is 100,90,90,80 number of gold medals. Is ranked as 1,2,2,4.

A blank line after each test.
Sample input
. 4. 4
. 4 1. 8
. 6. 6 2
. 4. 8 2
2 12 is. 4
0 1 2. 3
. 4 2
. 8 10 1
. 8. 11 2
. 8. 3 12 is
. 8. 4 13 is
0. 3
Sample Output
1: 3
1: 1
2: 1
1 :2

1: 1
1: 1
prompts
this question to be solved in the Olympic Games is the most favorable countries are ranked and ranking. Sorting can be as long as five times. First reads the countries information, write country code, data computing and storage rankings need. Then press the four kinds are ranked, respectively, ranking the country needs to rank and record the rankings. Finally, using the country code of the country rank. So that you can output the results.

#include<iostream>
#include<algorithm>
using namespace std;
struct node{
 double g,j,p;
 int id;
}c[100];
bool cmpg(node x,node y)
{
 return x.g>y.g;
}
bool cmpj(node x,node y)
{
 return x.j>y.j;
}
bool cmpgp(node x,node y)
{
 return x.g/x.p>y.g/y.p;
}
bool cmpjp(node x,node y)
{
 return x.j/x.p>y.j/y.p;
}
int main()
{
 int n,m;
 int need[100],rank[100][5];
 while(cin>>n>>m)
 {
  for(int i=0;i<n;i++)
  {
   cin>>c[i].g>>c[i].j>>c[i].p;
   c[i].id=i;
  }
  for(int i=0;i<m;i++)
   cin>>need[i];
  //接下来分别对四种情况进行排序,结果存在rank里 
  sort(c,c+n,cmpg);
  rank[c[0].id][1]=1;
  for(int i=1;i<n;i++)
  {
   if(c[i].g==c[i-1].g)
    rank[c[i].id][1]=rank[c[i-1].id][1];
   else
    rank[c[i].id][1]=i+1;
  }//
  sort(c,c+n,cmpj);
  rank[c[0].id][2]=1;
  for (int i=1;i<n;i++)
  {
      if (c[i].j==c[i-1].j)
       rank[c[i].id][2]=rank[c[i-1].id][2];
      else
       rank[c[i].id][2]=i+1;
     }//
     sort(c,c+n,cmpgp);  
  rank[c[0].id][3]=1;
     for(int i=1;i<n;i++)
  {
      if (c[i].g/c[i].p==c[i-1].g/c[i-1].p)
       rank[c[i].id][3]=rank[c[i-1].id][3];
      else
       rank[c[i].id][3]=i+1;
     }//
     sort(c, c + n, cmpjp);
   rank[c[0].id][4] = 1;
  for (int i = 1; i < n; i++)
  {
   if (c[i].j / c[i].p == c[i - 1].j / c[i - 1].p)
    rank[c[i].id][4] = rank[c[i - 1].id][4];
      else
    rank[c[i].id][4] = i + 1;
  }//
  //特定国家排序 
  for(int i=0;i<m;i++) 
  {
   int minn=n+1,type=5;
   for(int t=1;t<=4;t++)
   {
    int temp=1;
    for(int j=0;j<m;j++)
     if(i!=j)
   if(rank[need[i]][t]>rank[need[j]][t])
    temp++;
    if(minn>temp)
     minn=temp,type=t; 
   } 
   cout<<minn<<":"<<type<<endl;
  }
  cout<<endl;
 }
 return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43540515/article/details/91040956