PAT_A1012 | The Best Rank

1012 The Best Rank (25point(s))

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

主要思路:

  1. 题中提到的C、M、E三门成绩和平均成绩A,我们应该将他们分别看待(直接理解为四门科目的成绩吧),且分别计算排名。那么考虑到输出时的优先级为: A > C > M > E,那么在设置和使用分数、排名数组时,应该将他们分别对应下标0,1,2,3。对于每个学生的数据,还是储存在结构体中,所有学生即组成一个结构体数组。

     

  2. 既然需要最好的排名,应该将四门成绩分别排序获取排名,然后对查询的学生依次(即按照科目优先级顺序)枚举其四科排名,选择排名最好的科目输出。对该结构体数组排序,可以直接使用c++"algorithm"库中的sort函数,具体如何使用可以看看这篇文章:排序算法 | sort函数的使用,获取排名则根据排序结果处理即可,可以单独写一个函数。

 

完整代码如下:

//
// Created by LittleCat on 2020/2/13.
//
#include <cstdio>
#include <algorithm>
using namespace std;

#define NUM_OF_CURSE 4
#define NUM_OF_STU 2005

int now; //cmp中使用,表示成绩对应下标 now 排序(成绩的下标对应的是课程)
char curse[NUM_OF_CURSE] = {'A', 'C', 'M', 'E'};  //按照优先级顺序将课程排序,方便输出
struct node {
    int id;
    int grade[NUM_OF_CURSE];   //分别对应 A、C、M、E的成绩
    int rank[NUM_OF_CURSE];  //分别对应 A、C、M、E的排名
} stu[NUM_OF_STU];

/* 排名函数
 * 传入参数:
 *    课程编号 curse(0、1、2、3分别对应 A、C、M、E)
 *    排名总人数 n */
void getRank(int curse, int n) {
    stu[0].rank[curse] = 1;
    for (int i = 1; i < n; i++) {
        if (stu[i].grade[curse] == stu[i - 1].grade[curse])
            stu[i].rank[curse] = stu[i - 1].rank[curse];
        else
            stu[i].rank[curse] = i + 1;
    }
}

/* 查找函数
 * 传入参数:
 *    待查找的 id、查找范围的总人数n
 * 返回参数:
 *    如果找到返回对应下标,否则返回-1*/
int search(int id, int n) {
    for (int i = 0; i < n; i++) {
        if (stu[i].id == id)
            return i;
    }
    return -1;
}

/* 计算最好的排名对应的课程
 * 传入参数:
 *     待计算的学生下标index
 * 返回参数
 *     该学生最好的排名对应的课程(0、1、2、3分别对应 A、C、M、E)*/
int getBestCurse(int index) {
    int t = NUM_OF_STU, bestCurse;
    for (int i = 0; i < NUM_OF_CURSE; i++) {
        if (stu[index].rank[i] < t) {
            t = stu[index].rank[i];
            bestCurse = i;
        }
    }
    return bestCurse;
}

bool cmp(struct node x, struct node y) {
    return x.grade[now] > y.grade[now];
}


int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d %d %d %d", &stu[i].id, &stu[i].grade[1], &stu[i].grade[2], &stu[i].grade[3]);
        stu[i].grade[0] = (stu[i].grade[1] + stu[i].grade[2] + stu[i].grade[3]) / 3;
    }

    /* 分别给四门课程排序且赋予排名 */
    for(int i = 0; i < NUM_OF_CURSE; i++) {
        now = i;
        sort(stu, stu + n, cmp);
        getRank(now, n);
    }

    for (int i = 0; i < m; i++) {
        int id;
        scanf("%d", &id);

        int index = search(id, n);  //查找该id对应的下标
        //不存在此学生
        if (index == -1) {
            printf("N/A\n");
            continue;
        }

        int bestCurse = getBestCurse(index);
        printf("%d %c\n", stu[index].rank[bestCurse], curse[bestCurse]);
    }

}

 



 

end 

欢迎关注个人公众号 鸡翅编程 ”,这里是认真且乖巧的码农一枚。

---- 做最乖巧的博客er,做最扎实的程序员 ----

旨在用心写好每一篇文章,平常会把笔记汇总成推送更新~

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值