【PAT甲级题解记录】1025 PAT Ranking (25 分)

【PAT甲级题解记录】1025 PAT Ranking (25 分)

前言

Problem:1025 PAT Ranking (25 分)

Tags:结构体排序

Difficulty:剧情模式 想流点汗 想流点血 死而无憾

Address:1025 PAT Ranking (25 分)

问题描述

PAT 考试有不同考场,给出不同考场的学生成绩,要求汇总后根据成绩高低输出考生id、考场号、考场排名、总排名。

解题思路

  1. 我们写一个结构体用来存储学生信息:id、考场号、成绩、考场排名、总排名,结构体排序以成绩降序为第一顺序,考场id为第二顺序。
  2. 输入每个考场的学生信息,包括id、考场号和成绩。然后对这个考场的考生成绩排序,拍完序后可以得到这个考场每个学生考场排名。
  3. 最后把所有学生一起排序,同理得到每个学生的总排名。

参考代码

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;
int N; // the number of test locations
int K; // the number of testees
struct Testee {
    string registration_number;
    int score;
    int final_rank, location_number, local_rank;

    bool operator<(const Testee &t) const {
        return score == t.score ? registration_number < t.registration_number : score > t.score;
    }
};
// 选择结构体内部重载运算符,或者外部定义sort参数cmp都可以
bool cmp(Testee &t1, Testee &t2) {
    return t1.score == t2.score ? t1.registration_number < t2.registration_number : t1.score > t2.score;
}

vector<Testee> final_testees;
vector<Testee> local_testees;

void init() {
    cin >> N;

    // local
    for (int i = 1; i <= N; ++i) {
        // 输入基本信息
        cin >> K;
        local_testees.clear();
        for (int j = 0; j < K; ++j) {
            Testee testee;
            cin >> testee.registration_number >> testee.score;
            testee.location_number = i;
            local_testees.push_back(testee);
        }
        // 地区排序
        sort(local_testees.begin(), local_testees.end(), cmp); // 写了内部运算符重载就不需要加cmp了,下同
        // 更新地区排名,并初始化final考生数组
        local_testees[0].local_rank = 1;
        final_testees.push_back(local_testees[0]); // 将更新了地区排名的考生放入final考生数组
        for (int j = 1; j < K; ++j) {
            if (local_testees[j].score == local_testees[j - 1].score) {
                local_testees[j].local_rank = local_testees[j - 1].local_rank;
            } else {
                local_testees[j].local_rank = j + 1;
            }
            final_testees.push_back(local_testees[j]); // 将更新了地区排名的考生放入总数组
        }
    }
    // final
    sort(final_testees.begin(), final_testees.end(), cmp);

    final_testees[0].final_rank = 1;
    for (int i = 1; i < int(final_testees.size()); ++i) {
        if (final_testees[i].score == final_testees[i - 1].score) {
            final_testees[i].final_rank = final_testees[i - 1].final_rank;
        } else {
            final_testees[i].final_rank = i + 1;
        }
    }
}

void solution_1025() {
    init();
    cout << final_testees.size() << endl;
    for (int i = 0; i < int(final_testees.size()); ++i) {
        cout << final_testees[i].registration_number << " " << final_testees[i].final_rank << " "
             << final_testees[i].location_number << " " << final_testees[i].local_rank << endl;
    }
}

int main() {
    solution_1025();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值