题目描述如下
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<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct student{
char id[15];
int score=0;
int group = 0;
int org_rank=0;
} student_info;
bool cmp(student_info a, student_info b){
if(a.score != b.score){
return a.score > b.score;
}
// if else(a.group!=b.group){
// return a.group<b.group;
// }
else{
return strcmp(a.id,b.id)<0;
}
}
int main(){
int N;
scanf("%d",&N);
student_info student_info_list[N*301];
int group[N+1];
int num=1;
for(int i=1; i<=N; i++){
// int k;
scanf("%d",&group[i]);
int j=1;
for(j=1;j<= group[i];j++){
scanf("%s %d",student_info_list[num].id,&student_info_list[num].score);
// printf("%s\n",student_info_list[num].id);
student_info_list[num].group=i;
student_info_list[num].org_rank = j;
num++;
}
}
// cout<<"have done input"<<endl;
cout<<num-1<<endl;
sort(student_info_list+1,student_info_list+num,cmp);
int last_score=0,last_score_id=1;
int last_score_group[N+1],last_score_id_group[N+1],num_group[N+1];
memset(last_score_group,0,sizeof(last_score_group));
memset(last_score_id_group,1,sizeof(last_score_id_group));
memset(num_group,0,sizeof(num_group));
for(int i=1;i<num;i++){
num_group[student_info_list[i].group]++;
if(student_info_list[i].score != last_score){
last_score_id = i;
last_score = student_info_list[i].score;
}
if(last_score_group[student_info_list[i].group]!=student_info_list[i].score){
last_score_id_group[student_info_list[i].group]=num_group[student_info_list[i].group];
last_score_group[student_info_list[i].group]=student_info_list[i].score;
}
printf("%s %d %d %d",student_info_list[i].id, last_score_id,student_info_list[i].group,last_score_id_group[student_info_list[i].group]);
if(i!=num-1){
printf("\n");
}
}
return 0;
}
提交结果:卡在第三个测试点(id为2的测试点)。
问题分析
在我的代码逻辑里面,默认所考生分数大于零,这时在每个判断初始,都会将全局,考场排名初始化为1(预设为0)。
但是有可能出现,所有考生的成绩全为0,在这时,输出的考生排名全为0。
所以需要将,考生的初始排名设为1或者将第一名的考生的排名设为1.
代码修改
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct student{
char id[15];
int score=0;
int group = 0;
int org_rank=0;
} student_info;
bool cmp(student_info a, student_info b){
if(a.score != b.score){
return a.score > b.score;
}
// if else(a.group!=b.group){
// return a.group<b.group;
// }
else{
return strcmp(a.id,b.id)<0;
}
}
int main(){
int N;
scanf("%d",&N);
student_info student_info_list[N*301];
int group[N+1];
int num=1;
for(int i=1; i<=N; i++){
// int k;
scanf("%d",&group[i]);
int j=1;
for(j=1;j<= group[i];j++){
scanf("%s %d",student_info_list[num].id,&student_info_list[num].score);
// printf("%s\n",student_info_list[num].id);
student_info_list[num].group=i;
student_info_list[num].org_rank = j;
num++;
}
}
// cout<<"have done input"<<endl;
cout<<num-1<<endl;
sort(student_info_list+1,student_info_list+num,cmp);
int last_score=0,last_score_id=1; //初始化全局排名为1
int last_score_group[N+1],last_score_id_group[N+1],num_group[N+1];
memset(last_score_group,0,sizeof(last_score_group));
memset(last_score_id_group,0,sizeof(last_score_id_group));
memset(num_group,0,sizeof(num_group));
for(int i=1;i<num;i++){
num_group[student_info_list[i].group]++;
if(student_info_list[i].score != last_score ){
last_score_id = i;
last_score = student_info_list[i].score;
}
if(last_score_group[student_info_list[i].group]!=student_info_list[i].score || num_group[student_info_list[i].group]==1){ //将考场第1考生的排名初始化为1
last_score_id_group[student_info_list[i].group]=num_group[student_info_list[i].group];
last_score_group[student_info_list[i].group]=student_info_list[i].score;
}
printf("%s %d %d %d",student_info_list[i].id, last_score_id,student_info_list[i].group,last_score_id_group[student_info_list[i].group]);
if(i!=num-1){
printf("\n");
}
}
return 0;
}