PTA甲级 1012 The Best Rank (C++)

本文介绍了一种评估计算机科学一年级学生综合表现的方法,仅考虑三门课程成绩——C语言编程、数学(微积分或线性代数)、英语。通过比较学生的总平均分和各科排名,确定他们的最优级别。输出每个查询学生在各课程和平均分中的最高排名。
摘要由CSDN通过智能技术生成

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 N N and M ( ≤ 2000 ) M (≤2000) M(2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N N 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 M M lines, each containing a student ID.

Output Specification:

For each of the M M 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

Smaple Output:

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

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-08-11 13:37:18
// All rights reserved.

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cmath>

using namespace std;

bool cmp(pair<string, int>& x, pair<string, int>& y){
    return x.second > y.second;
}


int main(){
    int n, m;
    scanf("%d %d", &n, &m);
    unordered_map<string, vector<int>> stu;
    // stu vector<int> 里的含义:
    // [0] 表示 c 成绩
    // [1] 表示 m 成绩
    // [2] 表示 e 成绩
    // [3] 表示 a 成绩
    // [4] 表示 最好的排名
    // [5] 表示最好排名对应的类别(0 表示 a,1 表示 c,2 表示 m,3 表示 e)
    vector<pair<string, int>> cRank, mRank, eRank, aRank;

    for(int i = 0; i < n; ++i){
        string tmp;
        int c, m, e, a;

        cin >> tmp;
        cin >> c >> m >> e;
        a = round((c + m + e) / 3.0);

        stu[tmp].push_back(c);
        stu[tmp].push_back(m);
        stu[tmp].push_back(e);
        stu[tmp].push_back(a);

        cRank.emplace_back(tmp, c);
        mRank.emplace_back(tmp, m);
        eRank.emplace_back(tmp, e);
        aRank.emplace_back(tmp, a);
    }

    // 接下来要对四个 rank 排序,每对一个 rank 排好序就可以更新一下 stu 里的排名和对应的科目
    
    sort(aRank.begin(), aRank.end(), cmp);
    int rank = 1;
    stu[aRank[0].first].push_back(1); // 表示最好的排名是 1
    stu[aRank[0].first].push_back(0); // 表示最好排名对应的类别是 a
    for(int i = 1; i < n; ++i){
        if(aRank[i - 1].second > aRank[i].second) rank = i + 1;
        
        stu[aRank[i].first].push_back(rank);
        stu[aRank[i].first].push_back(0);
    }

    sort(cRank.begin(), cRank.end(), cmp);
    rank = 1;
    if(stu[cRank[0].first][4] > 1){
        stu[cRank[0].first][4] = 1;
        stu[cRank[0].first][5] = 1; // 表示最好排名对应的类别是 c
    }
    for(int i = 1; i < n; ++i){
        if(cRank[i - 1].second > cRank[i].second) rank = i + 1;
        
        if(stu[cRank[i].first][4] > rank){
            stu[cRank[i].first][4] = rank;
            stu[cRank[i].first][5] = 1; // 表示最好排名对应的类别是 c
        }
    }

    sort(mRank.begin(), mRank.end(), cmp);
    rank = 1;
    if(stu[mRank[0].first][4] > 1){
        stu[mRank[0].first][4] = 1;
        stu[mRank[0].first][5] = 2; // 表示最好排名对应的类别是 m
    }
    for(int i = 1; i < n; ++i){
        if(mRank[i - 1].second > mRank[i].second) rank = i + 1;
        
        if(stu[mRank[i].first][4] > rank){
            stu[mRank[i].first][4] = rank;
            stu[mRank[i].first][5] = 2; // 表示最好排名对应的类别是 m
        }
    }

    sort(eRank.begin(), eRank.end(), cmp);
    rank = 1;
    if(stu[eRank[0].first][4] > 1){
        stu[eRank[0].first][4] = 1;
        stu[eRank[0].first][5] = 3; // 表示最好排名对应的类别是 e
    }
    for(int i = 1; i < n; ++i){
        if(eRank[i - 1].second > eRank[i].second) rank = i + 1;
        
        if(stu[eRank[i].first][4] > rank){
            stu[eRank[i].first][4] = rank;
            stu[eRank[i].first][5] = 3; // 表示最好排名对应的类别是 e
        }
    }


    for(int i = 0; i < m; ++i){
        string check;
        cin >> check;

        if(stu.find(check) == stu.end()) printf("N/A\n");
        else{
            printf("%d ", stu[check][4]);
            switch(stu[check][5]){
                case 0: printf("A\n"); break;
                case 1: printf("C\n"); break;
                case 2: printf("M\n"); break;
                case 3: printf("E\n"); break;
                default: break;
            }
        }
    }

    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

负反馈循环

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值