九度[1007]-奥运排序问题

九度[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
解题思路:
复杂模拟
分别按各种方法,排序,输出的时候找到每个国家对应的最小排名和排名方式。
排序用快速排序实现, 需要注意的地方是快速排序的循环条件中,high和low至少要有一个是大等于或者小等于key,否则可能会出现无限循环。

AC代码:

#include <cstdio>
#include <map> 
using namespace std;
int N, M;
struct country{
    int id;
    int gold;
    int medal;
    int population;
    double goldRatio;
    double medalRatio;
}dat[500];
map<int, int> idRank1, idRank2, idRank3, idRank4;

double comp(int type, country a, country b){
    if(type == 0){
        return a.gold - b.gold;
    }
    if(type == 1){
        return a.medal - b.medal;
    }
    if(type == 2){
        return a.goldRatio - b.goldRatio;
    }
    if(type == 3){
        return a.medalRatio - b.medalRatio;
    }
}

void quickSort(country input[], int l, int r, int type){
    if(l >= r) return;
    int low = l, high = r;
    country key = input[l];
    while(low < high){
        while(low < high && comp(type, key, input[high]) >= 0) high--;
        if(low < high) input[low] = input[high];
        while(low < high && comp(type, input[low], key) > 0) low++;
        if(low < high) input[high] = input[low];
    }
    input[low] = key;
    quickSort(input, l, low-1, type);
    quickSort(input, high+1, r, type);
}

void bestRank(country x){
    int type, rank, id = x.id;
    if(idRank1[id]<=idRank2[id] && idRank1[id]<=idRank3[id] && idRank1[id]<=idRank4[id]){
        rank = idRank1[id];
        type = 1;
    }
    else if(idRank2[id]<idRank1[id] && idRank2[id]<=idRank3[id] && idRank2[id]<=idRank4[id]){
        rank = idRank2[id];
        type = 2;
    }
    else if(idRank3[id]<idRank1[id] && idRank3[id]<idRank2[id] && idRank3[id]<=idRank4[id]){
        rank = idRank3[id];
        type = 3;
    }
    else if(idRank4[id]<idRank1[id] && idRank4[id]<idRank2[id] && idRank4[id]<idRank3[id]){
        rank = idRank4[id];
        type = 4;
    }
    printf("%d:%d\n", rank, type);
}

int main(){
    freopen("C:\\Users\\Administrator\\Desktop\\test.txt", "r", stdin);
    while(scanf("%d%d", &N, &M) != EOF){
        for(int i = 0; i < N; i++){
            scanf("%d%d%d", &dat[i].gold, &dat[i].medal, &dat[i].population);
            dat[i].goldRatio = dat[i].gold / (1.0*dat[i].population);
            dat[i].medalRatio = dat[i].medal / (1.0*dat[i].population);
            dat[i].id = i;
        }
        country input[M];
        int indexList[M];
        int index;
        for(int i = 0; i < M; i++){
            scanf("%d", &index);
            input[i] = dat[index];
            indexList[i] = index;
        }
        //Sort && record Rank
        for(int i = 0; i < 4; i++){
            quickSort(input, 0, M-1, i);
            for(int j = 0; j < M; j++){
                if(i == 0) {
                    if(input[j].gold == input[j-1].gold && j != 0) idRank1[input[j].id] = idRank1[input[j-1].id];
                    else idRank1[input[j].id] = j+1;
                }
                if(i == 1) {
                    if(input[j].medal == input[j-1].medal && j != 0) idRank2[input[j].id] = idRank2[input[j-1].id];
                    else idRank2[input[j].id] = j+1;
                }
                if(i == 2) {
                    if(input[j].goldRatio == input[j-1].goldRatio && j != 0) idRank3[input[j].id] = idRank3[input[j-1].id];
                    else idRank3[input[j].id] = j+1;
                }
                if(i == 3) {
                    if(input[j].medalRatio == input[j-1].medalRatio && j != 0) idRank4[input[j].id] = idRank4[input[j-1].id];
                    else idRank4[input[j].id] = j+1;
                }
            }
        }
        //output
        for(int i = 0; i < M; i++){
            bestRank(dat[indexList[i]]);
        }
        printf("\n");
    }
    fclose(stdin);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,根据提供的引用内容,我无法回答关于西北工业大学计算机考研真题在百度云上的问题。引用提供了一个关于字符串合并的样例输入和输出,而引用和引用提供了一些与西北工业大学计算机研究生的相关信息。但是,这些引用内容都没有提供与百度云相关的信息。如果您有关于其他方面的问题,我将很乐意为您提供帮助和回答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [九度OJ 题目1471:合并符串](https://blog.csdn.net/SJF0115/article/details/8609716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [2021西北工业大学考研历年真题](https://blog.csdn.net/weixin_42502288/article/details/118177726)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [undefined](undefined)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值