题解 | #Repeater#

奥运排序问题

http://www.nowcoder.com/practice/100a4376cafc439b86f5f8791fb461f3

#include<bits/stdc++.h>
using namespace std;
struct Country{
    int num;//国家号
    int gold;//金牌数
    int medal;//奖牌数
    int population;//人口
};
int main(){
    int N = 0, M = 0;
    int m = 0;
    vector<Country> arr;
    vector<Country> res;//参与排序的国家
    Country c;
    while(cin >> N >> M){
        arr.clear();
        res.clear();
        for(int i = 0; i < N; i++){
            c.num = i;
            cin >> c.gold >> c.medal >> c.population;
            arr.push_back(c);
        }
        for(int i = 0; i < M; i++){
            cin >> m;
            res.push_back(arr[m]);
        }
        //统计四种方式的排名
        int rank[4] = {1};
        int bestrank = 0;
        for(int i = 0; i < M; i++){//每个国家
            for(int t = 0; t < 4; t++){//每种排名方式
                rank[t] = 1;
                for(int j = 0; j < M; j++){
                    if(t == 0){//方式0
                        if(res[j].gold > res[i].gold)rank[t]++;
                    }else if(t == 1){//方式1
                        if(res[j].medal > res[i].medal)rank[t]++;
                    }else if(t == 2){//方式2
                        float gpj = 0.00, gpi = 0.00;
                        if(res[j].gold != 0)gpj = (float)res[j].gold/res[j].population;
                        if(res[i].gold != 0)gpi = (float)res[i].gold/res[i].population;
                        if(gpj > gpi)rank[t]++;
                    }else{//方式3
                        float mpj = 0.00, mpi = 0.00;
                        if(res[j].medal != 0)mpj = (float)res[j].medal/res[j].population;
                        if(res[i].medal != 0)mpi = (float)res[i].medal/res[i].population;
                        if(mpj > mpi)rank[t]++;
                    }
                }
            }
            bestrank = 0;
            for(int t = 1; t < 4; t++){
                if(rank[t] < rank[bestrank])bestrank = t;
            }
            cout << rank[bestrank] << ':' << bestrank+1 << endl;
        }
        cout << endl;
        
    }
    return 0;
}
全部评论

相关推荐

中电45所 测试开发岗 可以解决北京户口,提供员工宿舍,早 8 晚 5(不过一般会加班到7-8点,周六一般也会去,面试官说的) 硕士
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务