题目要求:
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 85Sample 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分析
这一题不是很难,就是每个location内部排次序,然后k路归并再排一次序,很简单。就是在归并的时候需要多申请一个内存空间。另外就是排序的时候如果得分相同就比考号,小的在前。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _testee
{
char rgnum[14];
int lction;
int score;
int lcrank;
}testee;
int cmp(const void *a,const void *b);
void kmerge(testee t[100][300],int *k,int n,testee *f,int count);
int main(int argc,char* argv[])
{
int i,j,n,count,rank;
int k[100];
testee t[100][300],*final;
//freopen("input","r",stdin);
scanf("%d",&n);
count = 0;
for(i=0;i<n;i++)
{
scanf("%d",&k[i]);
count += k[i];
for(j=0;j<k[i];j++)
{
scanf("%s",t[i][j].rgnum);
t[i][j].lction = i+1;
scanf("%d",&t[i][j].score);
}
}
//make sure each testee's local rank
for(i=0;i<n;i++)
{
qsort(t[i],k[i],sizeof(testee),cmp);
t[i][0].lcrank = 1;
for(j=1;j<k[i];j++)
{
if(t[i][j].score == t[i][j-1].score)
t[i][j].lcrank = t[i][j-1].lcrank;
else
t[i][j].lcrank = j+1;
}
}
//merge
final = (testee *)malloc(sizeof(testee)*count);
kmerge(t,k,n,final,count);
printf("%d\n",count);
rank = 1;
printf("%s %d %d %d\n",final[0].rgnum,1,final[0].lction,final[0].lcrank);
for(i=1;i<count;i++)
{
if(final[i].score != final[i-1].score)
rank = i+1;
printf("%s %d %d %d\n",final[i].rgnum,rank,final[i].lction,final[i].lcrank);
}
free(final);
return 0;
}
int cmp(const void *a,const void *b)
{
const testee *t1,*t2;
t1 = a;
t2 = b;
if(t1->score != t2->score)
return t2->score - t1->score;
else
return strcmp(t1->rgnum,t2->rgnum);
}
void kmerge(testee t[100][300],int *k,int n,testee *f,int count)
{
int i,index,max_i;
int start[100];
testee max;
for(i=0;i<100;i++)
start[i] = 0;
index = 0;
while(index < count)
{
max.score = -1;
max_i = -1;
for(i=0;i<n;i++)
{
if(start[i]<k[i] && t[i][start[i]].score > max.score)
{
max = t[i][start[i]];
max_i = i;
}
else if(start[i]<k[i] && t[i][start[i]].score == max.score)
{
if(strcmp(t[i][start[i]].rgnum,max.rgnum) < 0)
{
max = t[i][start[i]];
max_i = i;
}
}
}
f[index++] = max;
start[max_i] += 1;
}
}