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.
注意事项
如果第三个测试点和第四个测试点过不去,代码中若使用了sort的话,看看sort的范围是否大于实际需要排序的范围,大于实际的范围,示例也能过,但最后两个测试点过不了。
代码
#include<iostream>
#include "algorithm"
#include "cstring"
struct student {
char id[15];
int score;
int rank;
int org;
int orgRank;
} stu[30010];
bool cmp (student a, student b) { //以a为基准
if (a.score != b.score) return a.score > b.score; //按分数从大到小排
else return strcmp(a.id,b.id)<0;
}
int main() {
int n,num=0;
scanf("%d",&n);
for (int j=1; j<=n;j++) {
int m;
scanf("%d",&m);
for (int i =0; i<m;i++) {
scanf("%s %d",stu[num].id,&stu[num].score);
stu[num].org = j;
num++;
}
std::sort(stu + num - m,stu + num,cmp);
stu[num - m].orgRank = 1;
int k =2;
for(int index = num-m +1;index<num;index++) {
if (stu[index].score == stu[index - 1].score ) {
stu[index].orgRank = stu[index-1].orgRank;
} else {
stu[index].orgRank=k;
}
k++ ;
}
}
std::sort(stu,stu + num, cmp);
stu[0].rank = 1;
printf("%d\n",num);
printf("%s %d %d %d\n",stu[0].id,stu[0].rank,stu[0].org,stu[0].orgRank);
int k =2;
for (int i = 1; i < num; ++i) {
if (stu[i].score == stu[i - 1].score ) {
stu[i].rank = stu[i-1].rank;
} else {
stu[i].rank=k;
}
k++ ;
printf("%s %d %d %d\n",stu[i].id,stu[i].rank,stu[i].org,stu[i].orgRank);
}
return 0;
}
本文围绕PAT排名合并程序展开,介绍了输入输出规范,输入包含测试地点数、各地点考生数及考生信息,输出需按最终排名升序排列。同时提醒若使用sort排序,范围不能大于实际需要排序的范围,否则部分测试点无法通过,并给出了相关代码。

162

被折叠的 条评论
为什么被折叠?



