zoukankan      html  css  js  c++  java
  • 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;
    }
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    POJ1064 Cable master(二分 浮点误差)
    Codeforces1113F. Sasha and Interesting Fact from Graph Theory(组合数学 计数 广义Cayley定理)
    Codeforces1100F. Ivan and Burgers(离线+线性基)
    Codeforces1097D. Makoto and a Blackboard(数论+dp+概率期望)
    Codeforces1099F. Cookies(线段树+dp+贪心+博弈)
    python-zx笔记4-文件操作
    python-zx笔记3-函数
    python-zx笔记2-help
    fiddler使用笔记1
    理解OAuth 2.0
  • 原文地址:https://www.cnblogs.com/itdef/p/14398609.html
Copyright © 2011-2022 走看看