zoukankan      html  css  js  c++  java
  • PAT甲级1141PAT Ranking of Institutions

    题目链接

    https://pintia.cn/problem-sets/994805342720868352/problems/994805344222429184

    题解

    题目要求

    n个考生,每个考生信息为:

    • ID

      6个字符的字符串,第一位是考试等级(B、A、T)

    • score

      分数,[0,100]的整数

    • school

      不超6位的学校码,由英文字母组成,不区分大小写,但输入中有大小写

    请输出:

    • 第一行输出学校的数量

    • 然后按rank的非降序输出Rank School TWS Ns

      • rank是排名,从1开始,根据TWS非升序排序。

        如果TWS相等,则两校的rank也相等,且应按NS增序输出;如果Ns也相等,则按学校码的字母表顺序输出

      • school是学校码,用小写输出

      • TWS是加权分,等于该校B级考生总分/1.5+该校A级考生总分+该校T级考生总分*1.5,只取计算结果的整数部分

      • Ns是该校的考生数

    思路

    • 注意将学校码转换为小写

    • 输出顺序有两方面:

      • 如果TWS相等,则两校的rank也相等,这一点要在最后控制
      • 其它输出顺序通过sort函数使用的cmp函数控制
    • 我这里是使用了一个map,最后存入vector排序;也可以使用两个map(键都为学校码),一个存每个TWS,一个存Ns,最后都存入vector

      我计算TWS的方法比较麻烦,因为刚开始理解错了TWS的公式

    • 用map而不是unorder_map的话,最后一个测试点会超时

    代码

    // Problem: PAT Advanced 1141
    // URL: https://pintia.cn/problem-sets/994805342720868352/problems/994805344222429184
    // Tags: map unordered_map sort
    
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <unordered_map>
    #include <vector>
    using namespace std;
    
    struct School{
        string code;  // 小写的学校码
        double testeeScoreSums[3] = {0, 0, 0};
        int TWS;
        int Ns = 0;
        
        void calcTWS(){
            this->TWS = testeeScoreSums[0] / 1.5 + testeeScoreSums[1] + testeeScoreSums[2] * 1.5;
        }
    };
    
    // 按照要求比较两个学校
    // 根据TWS非升序排序。如果TWS相等,则按Ns增序输出;如果Ns也相等,则按学校码的字母表顺序输出
    bool schoolCmp(School &s1, School &s2){
        if (s1.TWS == s2.TWS)
            if (s1.Ns == s2.Ns)
                return s1.code < s2.code;
            else
                return s1.Ns < s2.Ns;
        else
            return s1.TWS > s2.TWS;
    }
    
    int main()
    {
        int n, score; // the number of testees, the score of a testee
        string id, schoolCode, testLevels="BAT"; // the id of a testee, the school of a testee, three test levels
        unordered_map<string, School> schoolsMap;  // 键为学校码,值为学校
    
        // 获取输入
        cin >> n;
        for (int i = 0; i < n; i++){
            cin >> id >> score >> schoolCode;
            transform(schoolCode.begin(), schoolCode.end(), schoolCode.begin(), ::tolower);  // 将学校码转为小写
            schoolsMap[schoolCode].code = schoolCode;
            for (int j = 0; j < 3; j++)
                if(id[0] == testLevels[j]){
                    schoolsMap[schoolCode].testeeScoreSums[j] += score; // 分别统计该校三个等级考生的总分
                    schoolsMap[schoolCode].Ns += 1;  // 统计该校考生数
                    break;
                }
        }
    
        // 将学校存入vector并排序
        vector<School> schools;
        for (unordered_map<string, School>::iterator it = schoolsMap.begin(); it != schoolsMap.end(); it++){
            it->second.calcTWS();
            schools.push_back(it->second);
        }
        sort(schools.begin(), schools.end(), schoolCmp);
    
        // 输出结果,如果TWS相等,则两校的rank也相等,其它输出顺序要求已通过schoolCmp控制
        int schoolNum = schools.size(), rank = 1;
        printf("%d
    %d %s %d %d
    ", schoolNum, rank, schools[0].code.c_str(), schools[0].TWS, schools[0].Ns);
        for (int i = 1; i < schoolNum; i++){
            if(schools[i].TWS < schools[i-1].TWS)
                rank = i+1;
            printf("%d %s %d %d
    ", rank, schools[i].code.c_str(), schools[i].TWS, schools[i].Ns);
        }
        return 0;
    }
    

    作者:@臭咸鱼

    转载请注明出处:https://www.cnblogs.com/chouxianyu/

    欢迎讨论和交流!


  • 相关阅读:
    hdu 3648 Median Filter (树状数组)
    poj 1470 Closest Common Ancestors (LCA)
    poj 1330 Nearest Common Ancestors (LCA)
    hdu 2586 How far away ? (LCA)
    poj 3253 Fence Repair (哈夫曼)
    poj 1094 Sorting It All Out (拓扑排序)
    hdu 2527 Safe Or Unsafe (哈夫曼树)
    三层架构
    SQL2008、SQL2013 执行Transact-SQL 语句或者批处理时发生了异常。错误5120
    【MediaKit】WPF项目中 调用摄像头拍照的开发包
  • 原文地址:https://www.cnblogs.com/chouxianyu/p/13475314.html
Copyright © 2011-2022 走看看