题目描述:
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
我的思路:
这道题中,考生分多组输入,故不能直接开辟一个Student类数组,所以要用vector容器来存放每个学生的信息。
分数相同的考生排名相同,注意排名的计算(详见代码)
我的代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;
struct Student{//考生信息用结构体来存储
string registration_number;
int final_rank;
int location_number;
int local_rank;
int score;
};
vector<Student>answer;
bool Compare(Student a,Student b){
if(a.score!=b.score) return a.score>b.score;
else return a.registration_number<b.registration_number;
}
void toDealRank_part(vector<Student>&vs){//获得本考场内的排名
int len=vs.size();
vs[0].local_rank=1;
int cnt=2;
for(int i=1;i<len;i++){
if(vs[i].score==vs[i-1].score){
vs[i].local_rank=vs[i-1].local_rank;
}else{
vs[i].local_rank=cnt;
}
cnt++;
}
}
void toDealRank_total(vector<Student>&answer){//获得总排名
int len=answer.size();
answer[0].final_rank=1;
int cnt=2;
for(int i=1;i<len;i++){
if(answer[i].score==answer[i-1].score){
answer[i].final_rank=answer[i-1].final_rank;
}else{
answer[i].final_rank=cnt;
}
cnt++;
}
}
int main(){
int n;
cin>>n;
getchar();
vector<Student>vs[n+1];
for(int i=1;i<=n;i++){
int k;//每个考场的考生数
cin>>k;
while(k--){
string number;
int score;
cin>>number>>score;//输入准考证号和成绩
Student stu;
stu.registration_number=number;
stu.score=score;
stu.location_number=i;//对考场号进行复制
vs[i].push_back(stu);
}
//对该考场内考生进行排序,获得考生本考场内的排名
sort(vs[i].begin(),vs[i].end(),Compare);
toDealRank_part(vs[i]);
//将所有考生都加入到answer中
for(int q=0;q<vs[i].size();q++){
answer.push_back(vs[i][q]);
}
}
//对answer中的元素进行排序并算出最终排名
sort(answer.begin(),answer.end(),Compare);
toDealRank_total(answer);
int length=answer.size();
cout<<length<<endl;
for(int i=0;i<length;i++){
cout<<answer[i].registration_number<<" "<<answer[i].final_rank<<" "<<answer[i].location_number<<" "<<answer[i].local_rank<<endl;
}
return 0;
}