题目描述
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
代码长度限制 16 KB
时间限制 300 ms
内存限制 64 MB
题意
有n个考场,每个考场有若干数量的考生。现在给出各个考场中考生的准考证号与分数,要求将所有考生按分数从高到低排序,并按顺序输出所有考生的准考证号、排名、考场号以及考场内排名。
分析
而算法本体则分为下面三个部分:
步骤1:按考场读入各考生的信息,并对当前读入考场的所有考生进行排序。之后将该考场的所有考生的排名写入他们的结构体中。
步骤2:对所有考生进行排序。
步骤3:输出所有考生的信息。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
//定义结构体,用结构体存储考生信息
struct student {
char reg_num[20]; //准考证号
int score; //分数
int final_rank; //最终排名
int location_num; //考场号
int local_rank; //考场内排名
}stu[30010];
//用于传入sort的第三个参数,按要求进行排名
bool cmp(student a, student b)
{
if(a.score != b.score) return a.score > b.score;
else return strcmp(a.reg_num, b.reg_num) < 0;
}
int main()
{
int N, K;
int total_num = 0; //考生总数
scanf("%d", &N);
for (int i = 1; i <= N; i ++ )
{
scanf("%d", &K);
total_num += K; //更新考生总数
//录入信息(total_num - K代表该考场第一个考生)
for (int j = total_num - K; j < total_num; j ++ )
{
scanf("%s %d", stu[j].reg_num, &stu[j].score);
stu[j].location_num = i;
}
//各考场进行排序
sort(stu + total_num - K, stu + total_num, cmp);
//算出各考生考场内排名
stu[total_num - K].local_rank = 1;
for (int j = total_num - K + 1; j < total_num; j ++ )
{
//如果和上一考生同分则排名相同
if(stu[j].score == stu[j - 1].score)
stu[j].local_rank = stu[j - 1].local_rank;
else
stu[j].local_rank = j - total_num + K + 1;
}
}
//计算最终排名,与计算考场内排名方法类似
sort(stu, stu + total_num, cmp);
stu[0].final_rank = 1;
for (int i = 1; i < total_num; i ++ )
{
if(stu[i].score == stu[i - 1].score)
stu[i].final_rank = stu[i - 1].final_rank;
else
stu[i].final_rank = i + 1;
}
//输出所有信息
printf("%d\n", total_num);
for (int i = 0; i < total_num; i ++ )
{
printf("%s %d %d %d\n", stu[i].reg_num, stu[i].final_rank, stu[i].location_num, stu[i].local_rank);
}
return 0;
}