1025 PAT Ranking (25 分)解题思路

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805474338127872

思路

直接开辟30000个结构体数组(N<=100, K <= 300). 在每次输入完K个考生信息(ID, location_number, score)的后,对这局部的K的结构体进行sort排序,排序算法使用自定义的cmp函数(见下面代码),cmp使用了双关键字排序,如果分数相同,则按照ID进行升序排列。拍完这K个结构体后,再次遍历一遍,完善好信息(local_rank)

最后,对所有的结构体进行总的排序,算出总的排名,然后按照要求输出即可。

代码

#include <iostream>
#include <algorithm>
using namespace std;

struct testee {
    string ID;
    int location_number;
    int local_rank;
    int score;
}testees[30000];

bool cmp(struct testee testee1, struct testee testee2){
    if(testee1.score == testee2.score)
        return testee1.ID < testee2.ID;
    return testee1.score > testee2.score;
}

int main(){
    int N, K;
    int count = 0;	//测试者人数的计数
    int rank = 1;	//总排名
    cin >> N;
    for(int i=0; i<N; i++){
        cin >> K;
        for(int j=0; j<K; j++){
            cin >> testees[count].ID >> testees[count].score;
            testees[count].location_number = i+1;
            count ++;
        }
        sort(testees+count-K, testees+count, cmp);
        for(int m=count-K; m<=count-1; m++){
            testees[m].local_rank = m - count + K + 1;
        }
        for(int m=count-K; m<=count-2; m++){
            if(testees[m+1].score == testees[m].score)
                testees[m+1].local_rank = testees[m].local_rank;
        }
    }
    sort(testees, testees+count, cmp);
    cout << count << endl;
    cout << testees[0].ID << ' ' << rank << ' ' << testees[0].location_number << ' ' << testees[0].local_rank << endl;
    for(int i=1; i<count; i++){
        if(testees[i].score < testees[i-1].score)
            rank = i+1;
        cout << testees[i].ID << ' ' << rank << ' ' << testees[i].location_number << ' ' << testees[i].local_rank << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值