题目:1025 PAT Ranking (25 分)
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
思路:
1.用sort函数对结构体进行两次排序,一次是把同一个考场的人聚在一起然后排序,记下当前考场排名;第二次是对全部考生排名,记录下总分;然后按排名输出各个学生信息。
2.两种方式排序的sort函数需要重新定义排列规则。对同一个考场考生排序的时候设置为先根据考场排序(考场编号是固定的,可以从小到大排列),考场号一样按排名排列,分数一样则按照学号排序;对全部考生排序的话同理,不过少一个考场号的比较而已。
3.编程时考虑到每个考场人数,可以通过这个数据知道按考场排序时第几个考场在哪个位置
代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct student{
char reg_num[15];//考生号
int final_score;//总分
int final_rank;//总分排名
int loc_num;//考场号
int loc_rank;//当前考场排名
};
bool cmp_1(student A,student B){//分班级按班级排序
if(A.loc_num!=B.loc_num)
return A.loc_num<B.loc_num;
else if(A.final_score!=B.final_score)
return A.final_score>B.final_score;
else
return strcmp(A.reg_num,B.reg_num)<0;
}
bool cmp_2(student A,student B){//不分班级直接按总分排序
if(A.final_score!=B.final_score)
return A.final_score>B.final_score;
else
return strcmp(A.reg_num,B.reg_num)<0;
}
int main(){
student each[30010];
int n_l;//考场数
int n_s;//每个考场考生数
int a=0;//结构体数组的下标,最后用来算总数
int k=0;//记录每次换教室后已经经过了多少人,可以得到新教室中学生的排名
scanf("%d",&n_l);
for(int i=0;i<n_l;i++){
scanf("%d",&n_s);
for(int y=0;y<n_s;y++){
scanf("%s%d",&each[a].reg_num,&each[a].final_score);
each[a].loc_num=i+1;
a++;
}
}
sort(each,each+a,cmp_1);
each[0].loc_rank=1;
for(int j=1;j<a;j++){//遍历排序后每个学生的数据,为他们的考场排名初始化
if(each[j].loc_num==each[j-1].loc_num){
if(each[j].final_score==each[j-1].final_score){
each[j].loc_rank=each[j-1].loc_rank;
}
else{
each[j].loc_rank=j-k+1;
}
}
else{
k=j;
each[j].loc_rank=1;
}
}
sort(each,each+a,cmp_2);
each[0].final_rank=1;
for(int j=1;j<a;j++){//遍历排序后每个学生的数据,为他们的总分排名初始化
if(each[j].final_score==each[j-1].final_score){
each[j].final_rank=each[j-1].final_rank;
}
else{
each[j].final_rank=j+1;
}
}
printf("%d",a);
for(int l=0;l<a;l++){
printf("\n%s %d %d %d",each[l].reg_num,each[l].final_rank,each[l].loc_num,each[l].loc_rank);
}
return 0;
}
完美通过!
总结与疑问:
1.掌握了sort函数对排序题很有帮助
2.这并不是一次编译通过的,我第一次写的代码中的考生号是用长整型的。但此时在PAT官网上总不能编译通过。后来借鉴别人代码,把考生号改成字符串就立马可以编译通过了。不是很懂为什么。感觉长整型通过比较大小也是可以的吧,总是会有一个测试点不能通过,得22分。
希望有大佬指点一下。。
下面是考生号为长整型时的结果截图: