1025 PAT Ranking ( 25 )

博客围绕编写程序合并PAT考试排名展开,介绍输入输出规范。思路是对数据进行(n+1)次排序,使用C标准库qsort函数,结构体预留排名位置,排序后赋值。还详细介绍了qsort函数的参数、返回值等信息。

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

思路:

对数据实现 (n+1)次排序(n是每次输入完一组数据后排序,共排序n次,1是对最后总的排序),

使用C标准库函数 qsort( head , num , NodeSize , comper ) ,具体函数介绍我放到最后了。

结构体里就把本地排位和总排位排名的位置空出来,排序之后再对排名赋值。

代码:

#include <iostream>
#include <string.h>
#include <stdlib.h>

struct student{
    char id[15];
    int score,localRank,finalRank,place;

}student[100*300];

typedef struct student *Stu;

int cmp(const void *a,const void *b)
{
    int temp= ((Stu)a)->score-((Stu)b)->score;
    if(temp) return -temp;// a-b>0 -> a>b -> 即分数高的在后
    return strcmp( ((Stu)a)->id,((Stu)b)->id)>0 ;
    //change strcmp and temp fuctions of <0 and >0
}

int main()
{
    int n;
    scanf("%d",&n);
    int m[n],b=0,sum=0;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&m[b]);
        for(int j=sum;j<sum+m[b];j++)
        {
            scanf("%s %d",student[j].id,&student[j].score);
            student[j].place=i+1;
        }

        qsort(&student[sum],m[b],sizeof(student[0]),cmp);

        for(int j=sum;j<sum+m[b];j++)
        {
            if(j==sum||student[j].score!=student[j-1].score)
                student[j].localRank=j-sum+1;
            else
                student[j].localRank=student[j-1].localRank;
        }
        
        sum+=m[b];
        b++;
    }
    printf("%d\n",sum);
    qsort(student,sum,sizeof(student[0]),cmp);

    for(int j=0;j<sum;j++)
    {
        if(j==0||student[j].score!=student[j-1].score)
            student[j].finalRank=j+1;
        else
            student[j].finalRank=student[j-1].finalRank;
    }
    for(int j=0;j<sum;j++)
    {
        printf("%s %d %d %d\n",student[j].id,student[j].finalRank,student[j].place,student[j].localRank);
    }
}

qsort函数介绍:

qsort

Performs a quick sort.

void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) );

RoutineRequired HeaderCompatibility
qsort<stdlib.h> and <search.h>ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport library for MSVCRT.DLL, retail version

Return Value

None

Parameters

base

Start of target array

num

Array size in elements

width

Element size in bytes

compare

Comparison function

elem1

Pointer to the key for the search

elem2

Pointer to the array element to be compared with the key

Remarks

The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call:

compare( (void *) elem1, (void *) elem2 );

The routine must compare the elements, then return one of the following values:

Return ValueDescription
< 0elem1 less than elem2
0elem1 equivalent to elem2
> 0elem1 greater than elem2

The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值