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.
看样例来理解题目就可以了,关键还是qsort的使用和rank的获得,其他的就是一个输入输出罢了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct student
{
char ch[20];
int score;
int final_rank;
int loc_num;
int loc_rank;
}stu[50000];
int cmp(const void *a,const void *b){
if((*(struct student *)b).score!=(*(struct student *)a).score){//按分数递减
return (*(struct student *)b).score-(*(struct student *)a).score;
}
else{//按字典序递增
return strcmp((*(struct student *)a).ch,(*(struct student *)b).ch);
}
}
int main(){
int t;
scanf("%d",&t);
int sum=0;
int n=1;
int temp=1;
int rank=1;
for(int i=1;i<=t;i++){
cin>>n;
for(int j=0;j<=n-1;j++){
scanf("%s %d",stu[sum+j].ch,&stu[sum+j].score);
stu[sum+j].loc_num=i;
}
qsort(stu+sum,n,sizeof(stu[0]),cmp);
temp=stu[sum].score;
rank=1;
for(int q=sum;q<=sum+n-1;q++){
if(temp==stu[q].score){//与上一个一样则rank不变
stu[q].loc_rank=rank;
}
else{
rank=q-sum+1;//将rank更新为当前进行循环的次数
stu[q].loc_rank=rank;
temp=stu[q].score;
}
}
sum+=n;//更新sum
}
qsort(stu,sum,sizeof(stu[0]),cmp);//整体排序
temp=stu[0].score;
rank=1;
for(int q=0;q<=sum-1;q++){
if(temp==stu[q].score){
stu[q].final_rank=rank;
}
else{
rank=q+1;
stu[q].final_rank=rank;
temp=stu[q].score;
}
}
printf("%d\n",sum);
for(int i=0;i<=sum-1;i++){
printf("%s %d %d %d\n",stu[i].ch,stu[i].final_rank,stu[i].loc_num,stu[i].loc_rank );
}
return 0;
}