九度OJ——1007奥运会排序问题

题目描述:
按要求,给国家进行排名。
输入:
有多组数据。
第一行给出国家数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;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

daipuweiai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值