[PAT甲级]1025 PAT Ranking 详细题解+AC代码

[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 NNN (≤100\le 100100), the number of test locations. Then NNN ranklists follow, each starts with a line containing a positive integer KKK (≤300\le 300300), the number of testees, and then KKK 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 NNN. 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.


##题解1:(初次完成)
本题仅使用简单的排序即可,注意分组排序和全部排序下标的不同

#include <bits/stdc++.h>
using namespace std;
struct Student
{
    string numbers;
    int score;
    int local_rank;
    int final_rank;
    int location;
} Students[30010];
bool cmp(Student stu1, Student stu2)
{
    return stu1.score == stu2.score ? stu1.numbers < stu2.numbers : stu1.score > stu2.score;
}
int main()
{
    int N, K, NUM = 0;
    cin >> N;
    for (int i = 1; i <= N; i++)
    {
        cin >> K;
        for (int j = 0; j < K; j++)
        {
            cin >> Students[NUM].numbers >> Students[NUM].score;
            Students[NUM].location = i;
            NUM++;
        }
        sort(Students + NUM - K, Students + NUM, cmp);
        Students[NUM - K].local_rank = 1;
        for (int j = NUM - K + 1; j < NUM; j++)
        {
            Students[j].local_rank = Students[j].score == Students[j - 1].score ? Students[j - 1].local_rank : j - NUM + K + 1;
        }
    }
    sort(Students, Students + NUM, cmp);
    Students[0].final_rank = 1;
    for (int j = 1; j < NUM; j++)
    {
        Students[j].final_rank = Students[j].score == Students[j - 1].score ? Students[j - 1].final_rank : j + 1;
    }
    cout<<NUM<<endl;
    for(int i=0;i<NUM;i++)
    {
        cout<<Students[i].numbers<<" "<<Students[i].final_rank<<" "<<Students[i].location<<" "<<Students[i].local_rank<<endl;
    }
}

##题解2:(第二次完成)
使用vector完成

#include <bits/stdc++.h>
using namespace std;
class Students
{
public:
    long long int id;
    int location;
    int score;
    int local_rank;
    int all_rank;
};
int main()
{
    int N, K;
    cin >> N;
    vector<Students> All_students;   // 全体students
    vector<Students> Local_students; // 存储同一location的students
    for (int i = 1; i <= N; i++)
    {
        cin >> K;
        cin.ignore();
        for (int j = 0; j < K; j++)
        {
            Students stu;
            cin >> stu.id;
            cin >> stu.score;
            stu.location = i;
            Local_students.push_back(stu);
        }
        sort(Local_students.begin(), Local_students.end(), [](const Students &stu1, const Students &stu2)
             { return stu1.score == stu2.score ? stu1.id < stu2.id : stu1.score > stu2.score; });
        Local_students[0].local_rank = 1;
        All_students.push_back(Local_students[0]);
        for (int m = 1; m < Local_students.size(); m++)
        {
            if (Local_students[m].score == Local_students[m - 1].score)
            {
                Local_students[m].local_rank = Local_students[m - 1].local_rank;
            }
            else
            {
                Local_students[m].local_rank = m + 1;
            }
            All_students.push_back(Local_students[m]);
        }
        Local_students.clear();
    }
    sort(All_students.begin(), All_students.end(), [](const Students &stu1, const Students &stu2)
         { return stu1.score == stu2.score ? stu1.id < stu2.id : stu1.score > stu2.score; });
    cout<<All_students.size()<<endl;
    for (int m = 0; m < All_students.size(); m++)
    {
        if (m == 0)
        {
            All_students[m].all_rank = 1;
        }
        else
        {
            if (All_students[m].score == All_students[m - 1].score)
            {
                All_students[m].all_rank = All_students[m - 1].all_rank;
            }
            else
            {
                All_students[m].all_rank = m + 1;
            }
        }
       cout<<setfill('0')<<setw(13)<<All_students[m].id<<" "<<All_students[m].all_rank<<" "<<All_students[m].location<<" "<<All_students[m].local_rank<<endl;
    }
}


AC结果:
在这里插入图片描述


  • 26
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值