PAT甲级 1012 The Best Rank (25分)


To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A

思路

  • 本题主要在于数据的存取方式,先用 Stu 结构体数组来存储每个考生的 id 和用来存储其各科分数的数组,并对该结构体数组根据各科分数进行排序,每排序一次就将其排序结果写入排名数组 ranks,最后根据查询 id,输出最高排名信息即可
  • 以结构体 Stu 存放 id 和 score[4] 数组,score[0] ~ score[3] 分别代表 A,C,M,E
  • 由于 id 是 6 位的整数(6 digits),不妨设置 ranks [1000000] [4] 数组,其中 ranks [id] [0] ~ ranks [id] [4] 分别代表编号为 id 的考生的 A,C,M,E 分别的排名

注意点

  • 由于优先级是 A > C > M > E,为了方便枚举,设置数组时,尽量把 A 放在 C、M、E 前面
  • 本题 id 是整数(digits 即数字),不会是字符型
  • 本题没有说明平均分是向下取整还是四舍五入(向下取整也能通过),但为了更方便,平均分也可以用总分来代替
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 2000;

struct Stu {
    int id;
    int score[4] = {0};
} stus[maxn];

int n, m, q, ranks[1000000][4] = {0}, index;
char ch[4] = {'A', 'C', 'M', 'E'};

int cmp(Stu s1, Stu s2) {
    return s1.score[index] > s2.score[index];
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d", &stus[i].id);
        for (int j = 1; j <= 3; j++) {
            scanf("%d", &stus[i].score[j]);
            stus[i].score[0] += stus[i].score[j];
        }
    }
    for (index = 0; index < 4; index++) {
        sort(stus, stus + n, cmp);
        ranks[stus[0].id][index] = 1;
        for (int i = 1; i < n; i++) {
            if (stus[i].score[index] == stus[i - 1].score[index]) {
                ranks[stus[i].id][index] = ranks[stus[i - 1].id][index];
            } else {
                ranks[stus[i].id][index] = i + 1;
            }
        }
    }
    for (int i = 0; i < m; i++) {
        scanf("%d", &q);
        if (ranks[q][0] == 0) {
            printf("N/A\n");
        } else {
            int minRank = 2000, minIndex;
            for (int j = 0; j < 4; j++) {
                if (ranks[q][j] < minRank) {
                    minRank = ranks[q][j];
                    minIndex = j;
                }
            }
            printf("%d %c\n", minRank, ch[minIndex]);
        }
    }
    return 0;
}

文章作者: 吴鑫康
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 吴鑫康 !
评论
  目录