PAT A1025 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.
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct Student{
char id[20];///这里注意给出的测试数据比较长,如果设太短不足以存储所有的测试案例,会报错
int score;
int final_rank;
int local_num;//local_num
int local_rank;
}stu[30010];
bool cmp(Student a,Student b){
if(a.score !=b.score) return a.score>b.score;
else
return strcmp(a.id,b.id)<0;
}
int main(){
int local,testee;
int totalNum=0;
scanf("%d",&local);//输入的考场数
for(int i=1;i<=local;i++)//分别输入每个考场的信息
{
scanf("%d",&testee);//考场内人数
for(int j=0;j<testee;j++){
scanf("%s %d",stu[j+totalNum].id,&stu[j+totalNum].score);//Information of testee
stu[j+totalNum].local_num=i;//考场号
}
sort(stu+totalNum,stu+totalNum+testee,cmp);
//对考生按照要求进行排序,原则为cmp函数
stu[totalNum].local_rank=1;//第一个单独赋值
for (int k=totalNum+1;k<testee+totalNum;k++)
//此处注意起点终点即可
{
if(stu[k].score !=stu[k-1].score)
stu[k].local_rank=k+1-totalNum;
//注意考场内排名与循环变量的对应关系
else
stu[k].local_rank=stu[k-1].local_rank;
}
totalNum=testee+totalNum;
}
//final rank
sort(stu,stu+totalNum,cmp);
//output
printf("%d\n",totalNum);
stu[0].final_rank=1;
printf("%s %d %d %d\n",stu[0].id,stu[0].final_rank,stu[0].local_num,stu[0].local_rank);
for(int m=1;m<totalNum;m++){
if(stu[m].score!=stu[m-1].score)
stu[m].final_rank=m+1;
else
stu[m].final_rank=stu[m-1].final_rank;
printf("%s %d %d %d\n",stu[m].id,stu[m].final_rank,stu[m].local_num,stu[m].local_rank);
}
return 0;
}