PAT A 1025 PAT Ranking(C语言)

版权声明:本文为博主原创文章,欢迎交流学习,未经博主允许不得转载。 https://blog.csdn.net/qq_43749739/article/details/88035809

一、题目(Translated)

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.
编程能力测试(PAT)是浙江大学计算机科学与技术学院举办的一场考试。每场考试同时在多个地区进行,考试后,将尽快生成考试排名名单。现在要求你编写程序,正确的整合列表及生成排名。

输入格式:
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.
每个输入包含一个测试用例。每个测试用例,第一行给出一个正数N (≤100)为测试地区的数量。然后给出N个列表,每个列表第一行给出一个正数K (≤300)为该地区的考生信息数量,紧跟着K行考生信息,含:准考证号(13位数字);该考生总成绩。同一行的数字之间以空格分隔。

输出格式:
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.
考试地区从1至N编号。输出按总成绩非递减排序。总成绩相同者并列排名,并按准考证号非递减排序;

输入样例:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

输出样例:
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.

二、思路

STEP1:
建立考生结构体:{准考证号,总分,地区编号,总排名,地区排名};
STEP2:
建立考生数组A[30000];
循环K次,每次读入N[ i ]个考生信息;输入并添加地区编号,完成后对该地区的考生信息进行排序,按要求添加地区排名;
STEP3:
对所有数据排序,添加总排名信息;
STEP4:
输出;

三、代码实现

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
    char ID[13];
    int score,fRank,lRank,lNum;
}Testee;
Testee A[30000];
int cmp(const void *a,const void *b)
{
    if( (*(Testee *)a).score != (*(Testee *)b).score )
        return (*(Testee *)b).score - (*(Testee *)a).score;
    else return strcmp( (*(Testee *)a).ID , (*(Testee *)b).ID );
}
int main()
{
    int K;
    scanf("%d",&K);
    int N[K+1],sum=0;
    for(int i=1 ; i<=K ; i++)
    {
        scanf("%d",&N[i]);
        for(int j=0 ; j<N[i] ; j++)
        {
            scanf("%s %d",A[j+sum].ID,&A[j+sum].score);
            A[j+sum].lNum=i;
        }
        qsort(A+sum,N[i],sizeof(Testee ),cmp);
        for(int j=0,r=1 ; j<N[i] ; j++)
        {
            if( j && A[j+sum].score != A[j+sum-1].score )
                r=j+1;
            A[j+sum].lRank=r;
        }
        sum+=N[i];
    }
    qsort(A,sum,sizeof(Testee ),cmp);
    for(int i=0,r=1 ; i<sum ; i++)
    {
        if( i && A[i].score != A[i-1].score )
            r=i+1;
        A[i].fRank=r;
    }
    printf("%d%s",sum,sum ? "\n":"");
    for(int i=0 ; i<sum ; i++)
        printf("%s %d %d %d%s",A[i].ID,A[i].fRank,A[i].lNum,A[i].lRank,i==sum-1 ? "":"\n");
}

猜你喜欢

转载自blog.csdn.net/qq_43749739/article/details/88035809
今日推荐