PAT 甲级 1012 The Best Rank 模拟

地址 https://pintia.cn/problem-sets/994805342720868352/problems/994805502658068480

复制代码
主要是模拟 题意比较绕。
题目大意是 接受各个学生的三门成绩 C M E, 然后四舍五入计算出平均成绩A
在 接受询问的学生的id后 打印出该学生排名最佳的学科名次和学习名字
如果多门学科名次相同 则按照 ACME的次序排序考虑

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


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
复制代码

解答

复制代码
这里有个暗坑需要注意的是
如果某学科的成绩排序如下
100
99
99
99
85
那么该科目成绩99的三个学生都是排名第二 成绩85的学生排名第五
所以最好使用数组来记录成绩排序(允许有重复)
然后查询某个成绩的排名使用遍历找到第一个符合的分数的位置就是排名 或者使用二分查找第一个符合分数的位置作为排名
复制代码

 

复制代码
#include <iostream>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>

using namespace std;

const int N = 2010;

map<string, vector<int>> mm;
vector<int> mA; vector<int> mC; vector<int> mM; vector<int> mE;

int n, m;
//100 99 99 99 85
int GetIdxA(int x) {
    for (int i = 0; i < mA.size(); i++) { if (x == mA[i]) return i + 1; }
    return 0;
}

int GetIdxC(int x) {
    for (int i = 0; i < mC.size(); i++) { if (x == mC[i]) return i + 1; }
    return 0;
}

int GetIdxM(int x) {
    for (int i = 0; i < mM.size(); i++) { if (x == mM[i]) return i + 1; }
    return 0;
}

int GetIdxE(int x) {
    for (int i = 0; i < mE.size(); i++) { if (x == mE[i]) return i + 1; }
    return 0;
}


int main() {
    //首先获得学生成绩的个数和 查询 个数
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        //接受三门成绩和学生ID并且计算出平均成绩   规则是四舍五入
        string id; int C, M, E;
        cin >> id >> C >> M >> E;
        int A = round((C + M + E) / 3.0);
        mm[id].push_back(A);
        mm[id].push_back(C);
        mm[id].push_back(M);
        mm[id].push_back(E);
        mA.push_back(A); mC.push_back(C);
        mM.push_back(M); mE.push_back(E);
    }
    //各个学科成绩进行排序  从大到小
    sort(mA.begin(), mA.end(),greater<int>()); sort(mC.begin(), mC.end(), greater<int>());
    sort(mM.begin(), mM.end(), greater<int>()); sort(mE.begin(), mE.end(), greater<int>());

    for (int i = 0; i < m; i++) {
        //接受查询的学生id
        string id; cin >> id;
        if (mm.count(id) == 0) { cout << "N/A" << endl; continue; }

        int ans = 999999; char type = 'A';
        int idx = GetIdxA(mm[id][0]);
        if (ans > idx) { ans = idx; type = 'A'; }
        idx = GetIdxC(mm[id][1]);
        if (ans > idx) { ans = idx; type = 'C'; }
        idx = GetIdxM(mm[id][2]);
        if (ans > idx) { ans = idx; type = 'M'; }
        idx = GetIdxE(mm[id][3]);
        if (ans > idx) { ans = idx; type = 'E'; }

        cout << ans << " " << type << endl;
    }



    return 0;
}
复制代码

 

posted on   itdef  阅读(90)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示