【PAT甲级 - C++题解】1025 PAT Ranking

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1025 PAT Ranking (pintia.cn)
🔑中文翻译:PAT 排名
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

1025 PAT Ranking

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
题意

给定不同区域的学生的考号和成绩,让我们将所有成绩合并在一起,然后按成绩从高到低输出,如果成绩相同就按考号的升序输出。输出格式如下:

registration_number final_rank location_number local_rank

也就是输出考号,最终排名,地区编号,地区排名。

思路
  1. 先处理各区域的排名,学生的所有信息用结构体存起来,每个区域都用一个临时数组 temp 存起来,输入完后对 temp 进行排序,然后再去更新该区域学生的区域排名信息并放到总容器 all 中。
  2. 处理完各区域再处理总排名,所有学生信息都在总容器 all 中,对其进行排序,然后再更新每个学生的总排名信息。
  3. 最后按照格式输出总容器中的学生信息。
代码
#include<bits/stdc++.h>
using namespace std;

struct Student {
    string id;
    int grade;
    int location_number, final_rank, local_rank;
    bool operator < (const Student& s)const
    {
        if (grade != s.grade)    return grade > s.grade;
        return id < s.id;
    }
};

vector<Student> all;

int main()
{
    int n, k;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> k;
        vector<Student> temp;
        //输入基本信息
        for (int j = 1; j <= k; j++)
        {
            string id;
            int grade;
            cin >> id >> grade;
            temp.push_back({ id,grade,i });
        }

        sort(temp.begin(), temp.end());  //进行区域排名
        for (int j = 0; j < k; j++)    //更新区域排名信息
        {
            if (!j || temp[j].grade != temp[j - 1].grade)  //和前一个分数相同
                temp[j].local_rank = j + 1;
            else    //和前一个分数不同
                temp[j].local_rank = temp[j - 1].local_rank;
            all.push_back(temp[j]);
        }
    }

    sort(all.begin(), all.end());    //进行总排名
    for (int i = 0; i < all.size(); i++)   //更新总排名信息
    {
        if (!i || all[i].grade != all[i - 1].grade)  //和前一个分数相同
            all[i].final_rank = i + 1;
        else    //和前一个分数不同
            all[i].final_rank = all[i - 1].final_rank;
    }

    //输入总排名
    cout << all.size() << endl;
    for (auto& s : all)
    {
        cout << s.id << " " << s.final_rank << " " << s.location_number
            << " " << s.local_rank << endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值