PTA: 7-14 PAT ranking summary (C language)

The Computer Programming Ability Test (PAT) aims to objectively evaluate candidates' algorithm design and program design implementation capabilities through unified online exams and automatic evaluation methods, scientifically evaluate computer programming talents, and select talents for enterprises Provide reference standards (URL http://www.patest.cn).

Each test will be held in several different test centers at the same time, and each test center uses a local area network to generate the results of the test center. After the test, the results of each test center will be immediately summarized into a general ranking table.

Now please write a program to automatically merge the scores of each test center and generate a total ranking table.

Input format:

The first line of input gives a positive integer N (≤100), representing the total number of test centers. Then the results of N test centers are given in the format: first, a positive integer K (≤300) is given in one line, representing the total number of candidates in the test center; followed by K lines, each line gives the information of one candidate, including the test number (by 13-digit integer) and score (an integer in the interval [0,100]), separated by a space.

Output format:

First output the total number of candidates in the first line. Then output the summary ranking table, each candidate's information occupies one line, the order is: test number, final ranking, test center number, and rank at the test center. The test sites are numbered from 1 to N in the order given by the input. Candidates' output must be output in non-decreasing order of final ranking. Candidates who get the same score should have the same rank, and output in increasing order of test number.

Input sample:

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

Reference Code:

/**
 * 7-14 PAT排名汇总
 *  快速排序
 */
#include <stdio.h>
#include <string.h>

struct stu {
    char id[14];                //考号
    int score;                  //分数
    int kc;                     //考场
};
struct stu a[30000];

int bigger(const char *s1, const char *s2) {
    for (int i = 0; i < 13; i++)
        if (s1[i] > s2[i])
            return 1;
        else if (s1[i] < s2[i])
            return 0;
    return 1;
}

void qsort(int l, int r) {
    if (l >= r)
        return;

    int i = l;
    int j = r;

    struct stu t = a[l];
    while (i != j) {
        while (i < j && (a[j].score < t.score || a[j].score == t.score && bigger(a[j].id, t.id)))
            j--;
        while (i < j && (a[i].score > t.score || a[i].score == t.score && bigger(t.id, a[i].id)))
            i++;
        if (i < j) {
            struct stu s = a[i];
            a[i] = a[j];
            a[j] = s;
        }
    }
    a[l] = a[i];
    a[i] = t;

    qsort(l, i - 1);
    qsort(i + 1, r);

    return;
}

void Copy(int *b2, int *b1, int n) {
    for (int i = 1; i <= n; i++)
        b2[i] = b1[i];
}

int main() {
    int n, j, i, top = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        int k;
        scanf("%d", &k);
        for (j = 0; j < k; j++) {
            char id[14];
            int score;
            scanf("%s %d", id, &score);
            a[top].score = score;
            a[top].kc = i;
            strcpy(a[top].id, id);
            top++;
        }
    }
    qsort(0, top - 1);

    int levall = 1, b1[n + 1], b2[n + 1], score = a[0].score;

    for (i = 1; i <= n; i++)
        b1[i] = 1, b2[i] = 1;
    printf("%d\n", top);
    printf("%s %d %d %d\n", a[0].id, 1, a[0].kc, 1);
    int llevall = 1;            //上一个总排名
    levall = 2;                   //总排名

    Copy(b2, b1, n);
    b1[a[0].kc]++;
    for (i = 1; i < top; i++) {
        if (a[i].score == a[i - 1].score) {
            printf("%s %d %d %d\n", a[i].id, llevall, a[i].kc, b2[a[i].kc]);
            levall++;
            b1[a[i].kc]++;
        } else {
            printf("%s %d %d %d\n", a[i].id, levall, a[i].kc, b1[a[i].kc]);
            llevall = levall;
            levall++;

            Copy(b2, b1, n);
            b1[a[i].kc]++;                    //考场的排名
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_61228493/article/details/131108500