题目描述:
按要求,给国家进行排名。
输入:
有多组数据。
第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。
第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。
接下来一行给出M个国家号。
输出:
排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例
对每个国家给出最佳排名排名方式 和 最终排名
格式为: 排名:排名方式
如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例
如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.
每组数据后加一个空行。
样例输入:
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
样例输出:
1:3
1:1
2:1
1:2
1:1
1:1
思路:这是道结构体排序问题,对每种排序规则进行一次排序,并排排名记录下来,最后进行比对最优的排序名次。
AC代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct country Country;
struct country{
int gold; //金牌数
int medal; //奖牌数
int population; //人口数
double goldrate; //金牌比例
double medalrate; //奖牌比例
int rank[5]; //各种排名次序
int id;
};
int N,M,id;
vector<Country> countrys,countrysort;
int cmp_gold(Country c1,Country c2)
{
return c1.gold>c2.gold;
}
int cmp_medal(Country c1,Country c2)
{
return c1.medal>c2.medal;
}
int cmp_goldrate(Country c1,Country c2)
{
return c1.goldrate>c2.goldrate;
}
int cmp_medalrate(Country c1,Country c2)
{
return c1.medalrate>c2.medalrate;
}
int cmp_id(Country c1,Country c2)
{
return c1.id<c2.id;
}
int main()
{
while(scanf("%d %d",&N,&M) != EOF){
countrys.clear();
countrysort.clear();
//初始化数据
for(int i = 0 ; i < N ; i++){
Country c;
scanf("%d %d %d",&c.gold,&c.medal,&c.population);
c.goldrate = c.gold*1.0/c.population;
c.medalrate = c.medal*1.0/c.population;
countrys.push_back(c);
}
for(int i = 0 ; i < M ; i++){
scanf("%d",&id);
countrysort.push_back(countrys[id]);
countrysort[i].id = i;
}
//按金牌排序
sort(countrysort.begin(),countrysort.end(),cmp_gold);
for(int i = 0 ; i < N ; i++){
int rank = i;
while(rank>0&&countrysort[i].gold == countrysort[rank-1].gold){
rank--;
}
countrysort[i].rank[1] = rank+1;
}
//按奖牌排序
sort(countrysort.begin(),countrysort.end(),cmp_medal);
for(int i = 0 ; i < N ; i++){
int rank = i;
while(rank>0&&countrysort[i].medal == countrysort[rank-1].medal){
rank--;
}
countrysort[i].rank[2] = rank+1;
}
//按金牌比率排序
sort(countrysort.begin(),countrysort.end(),cmp_goldrate);
for(int i = 0 ; i < N ; i++){
int rank = i;
while(rank>0&&countrysort[i].goldrate == countrysort[rank-1].goldrate){
rank--;
}
countrysort[i].rank[3] = rank+1;
}
//按奖牌比率排序
sort(countrysort.begin(),countrysort.end(),cmp_medalrate);
for(int i = 0 ; i < N ; i++){
int rank = i;
while(rank>0&&countrysort[i].medalrate == countrysort[rank-1].medalrate){
rank--;
}
countrysort[i].rank[4] = rank+1;
}
sort(countrysort.begin(),countrysort.end(),cmp_id);
for(int i = 0 ; i < M ; i++){
int choice = 1;
int rank = countrysort[i].rank[1];
for(int j = 1 ; j <= 4 ; j++){
if(countrysort[i].rank[j] < rank){
choice = j;
rank = countrysort[i].rank[j];
}
}
cout<<rank<<":"<<choice<<endl;
}
cout<<endl;
}
return 0;
}