1025 PAT Ranking (25 分)(C语言实现)(PAT)(排序算法)

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

程序&算法

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MaxNum 30000
#define idLen 14

struct Ranklists{
    char idNumber[idLen];
    int score, final_rank, location, local_rank;
}Rank[MaxNum];

void Input(int N, int *size);
int comp(const void *a, const void *b);
void finalRanks(int size);
void localRankS(int N, int size);
void print(int size);
int main()
{
    int N, size = 0;
    scanf("%d", &N);
    Input(N, &size);
    qsort(Rank, size, sizeof(struct Ranklists), comp);
    finalRanks(size);
    localRankS(N,size);
    print(size);
    return 0;
}

void Input(int N, int *size)
{
    int K;
    for(int i=1; i<=N; i++ ){
        scanf("%d", &K);
        for(int j=0; j<K; j++ ){
            scanf("\n%s %d", Rank[*size].idNumber, &Rank[*size].score);
            Rank[*size].location = i;
            (*size)++;
        }
    }
}

int comp(const void *a, const void *b)
{ 
    int k;
    if ( ((const struct Ranklists*)a)->score >((const struct Ranklists*)b)->score )
        k = -1;
    else if ( ((const struct Ranklists*)a)->score < ((const struct Ranklists*)b)->score )
        k = 1;
    else {
        if (strcmp( ((const struct Ranklists*)a)->idNumber, ((const struct Ranklists*)b)->idNumber )>0)
            k = 1;
        else
            k = -1;
    }
    return k;
}

void finalRanks(int size)
{
    Rank[0].final_rank = 1;
    for(int i=1; i<size; i++){
        if(Rank[i].score == Rank[i-1].score) Rank[i].final_rank = Rank[i-1].final_rank;
        else Rank[i].final_rank = i+1;
    }
}

void localRankS(int N, int size)
{
    int cnt[N+1], temp[N+1], last[N+1];
    for(int i=1; i<=N; i++){
        cnt[i] = 0;
        temp[i] = -1;
        last[i] = -1;
    }
    for(int i=0; i<size; i++ ){
        cnt[Rank[i].location]++;
        if(Rank[i].score == temp[Rank[i].location]) Rank[i].local_rank = Rank[last[Rank[i].location]].local_rank;
        else Rank[i].local_rank = cnt[Rank[i].location];
        temp[Rank[i].location] = Rank[i].score;
        last[Rank[i].location] = i;
    }
}

void print(int size)
{
    printf("%d\n", size);
    for(int i=0; i<size; i++ )
        printf("%s %d %d %d\n", Rank[i].idNumber, Rank[i].final_rank, Rank[i].location, Rank[i].local_rank);
}
全部评论

相关推荐

11-28 17:48
中山大学 C++
点赞 评论 收藏
分享
11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务