奥运排序问题

该博客介绍了一种解决奥运国家排名问题的算法,包括根据金牌、奖牌总数以及人口比例进行排序。代码中处理了人口为0的特殊情况,避免了除以0的错误。通过比较不同指标下的排名,确定每个国家的最佳排名。示例展示了输入和输出格式,并给出了完整的C++代码实现。
摘要由CSDN通过智能技术生成

奥运排序问题

题目描述
按要求,给国家进行排名。
输入描述:
有多组数据。
第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。
第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。
接下来一行给出M个国家号。
输出描述:
排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例
对每个国家给出最佳排名排名方式 和 最终排名
格式为: 排名:排名方式
如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例
如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.
每组数据后加一个空行。

示例1
输入

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

难点:要注意对于人口等于0 的处理,这是本题最坑的点。除的时候如果分母为零,要按照分子讨论,分子为零则整体为零,否则置为正无穷(用一个较大的值即可)。

代码:

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<int,int> PII1;
typedef pair<double,int> PII2;
int n,m;

int get_rank1(int k,vector<PII1> m){
    int ans=0;
    for(int i=0;i<m.size();i++){
        if(m[i].second!=k){
            if(i==0||m[i].first!=m[i-1].first)
                ans=i+1;
        }
        else
        {
            if(i==0||m[i].first!=m[i-1].first)
                ans=i+1;
            break;
        }
    }
    return ans;
}



int get_rank2(int k,vector<PII2> m){
    int ans=0;
    for(int i=0;i<m.size();i++){
        if(m[i].second!=k){
            if(i==0||m[i].first!=m[i-1].first)
                ans=i+1;
        }
        else
        {
            if(i==0||m[i].first!=m[i-1].first)
                ans=i+1;
            break;
        }
    }
    return ans;
}


int main(){
    while(cin>> n >>m){
        vector<PII1> gold,medal;
        vector<PII2> gold_proportion,medal_proportion;
        for(int i=0;i<n;i++){
            int a,b,c;
            cin >> a >> b >> c;
            gold.push_back({a,i});
            medal.push_back({b,i});
            if(c!=0){
                gold_proportion.push_back({(double)a/c,i});
                medal_proportion.push_back({(double)b/c,i});
            }
            else{
                if(a==0)
                    gold_proportion.push_back({0.0,i});
                else
                    gold_proportion.push_back({0x3f,i});
                if(b==0)
                    medal_proportion.push_back({0.0,i});
                else
                    medal_proportion.push_back({0x3f,i});
            }
            
        }
        sort(gold.begin(),gold.end());
        reverse(gold.begin(),gold.end());
        sort(medal.begin(),medal.end());
        reverse(medal.begin(),medal.end());
        sort(gold_proportion.begin(),gold_proportion.end());
        reverse(gold_proportion.begin(),gold_proportion.end());
        sort(medal_proportion.begin(),medal_proportion.end());
        reverse(medal_proportion.begin(),medal_proportion.end());
        while(m--){
            int k;
            cin >> k;
            int t=get_rank1(k,gold);
            int way=1;
            if(t>get_rank1(k,medal)){
                t=get_rank1(k,medal);
                way=2;
            }
            if(t>get_rank2(k,gold_proportion)){
                t=get_rank2(k,gold_proportion);
                way=3;
            }
            if(t>get_rank2(k,medal_proportion)){
                t=get_rank2(k,medal_proportion);
                way=4;
            }
            printf("%d:%d\n",t,way);
        }
        printf("\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值