九度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

这题要注意的是排名是在那M个国家之间的排名,与其他国家无关。另外每组数据后要输出一个空行,不然会 Presentation Error。
源代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
 
int main(){
    int N, M;
    while(cin >> N >> M){
        int *goldMedal = new int[N];
        int *medal = new int[N];
        int *population = new int[N];
        double *goldMedalRate = new double[N];
        double *medalRate = new double[N];
        int *country = new int[M];
        //输入条件,并计算比例
        for(int i=0; i<N; i++){
            cin >> goldMedal[i];
            cin >> medal[i];
            cin >> population[i];
            goldMedalRate[i] = (double) goldMedal[i] / (double) population[i];
            medalRate[i] = (double) medal[i] / (double) population[i];
        }
        for(int i=0; i<M; i++){
            cin >> country[i];
        }
        //计算排名
        for(int i=0; i<M; i++){
            int bestRanking = M + 1;
            int rankingMethod = 0;
            int ranking[4] = {1, 1, 1, 1};
            for(int j=0; j<M; j++){
                if(goldMedal[country[j]] > goldMedal[country[i]]) ranking[0] ++;
                if(medal[country[j]] > medal[country[i]]) ranking[1] ++;
                if(goldMedalRate[country[j]] > goldMedalRate[country[i]]) ranking[2] ++;
                if(medalRate[country[j]] > medalRate[country[i]]) ranking[3] ++;
            }
            for(int k=0; k<4; k++){
                if(bestRanking > ranking[k]){
                    bestRanking = ranking[k];
                    rankingMethod = k + 1;
                }
            }
            cout << bestRanking << ':' << rankingMethod << endl;
        }
        cout << endl;
    }
 
    return 0;
}

  

posted on   C_haos  阅读(248)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示