✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
专栏地址:PAT题解集合
原题地址:题目详情 - 1025 PAT Ranking (pintia.cn)
中文翻译:PAT 排名
专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞收藏,您的支持就是我创作的最大动力
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
也就是输出考号,最终排名,地区编号,地区排名。
temp
存起来,输入完后对 temp
进行排序,然后再去更新该区域学生的区域排名信息并放到总容器 all
中。all
中,对其进行排序,然后再更新每个学生的总排名信息。#include
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;
}