zoukankan      html  css  js  c++  java
  • PAT甲级1153Decode Registration Card of PAT

    题目链接

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

    题解

    题目要求和思路

    PAT注册号分4个部分:

    • 第1位字母代表考试等级

    • 第2到4位数字是考试地点,大小从101到999

    • 第5到10位数字为考试日期

    • 第11到13位数字是考生号,大小从000到999

    • 1:给定考试等级,要求输出该考试等级所有考生的注册号和分数(注册号升序,分数非升序)

      遍历找出指定考试等级的考生,然后排序输出

    • 2:给定考试地点,要求输出该考试地点中考生数和考生分数之和

      遍历找出指定考试地点的考生,然后统计数据

    • 3:给定考试时间,要求输出每个考试地点的考生数(考试地点升序,考生数非升序)

      遍历考生, 统计每个考试地点的考生数到unordered_map里(统计到map里会超时),键是考试地点,值是考生数,然后输出(我这里是先存入vector再输出)

    代码

    // Problem: PAT Advanced 1153
    // URL: https://pintia.cn/problem-sets/994805342720868352/problems/1071785190929788928
    // Tags: map unordered_map sort
    
    #include <iostream>
    #include <vector>
    #include <string>
    #include <unordered_map>
    #include <algorithm>
    using namespace std;
    
    // 保存参试者的信息和最终要输出的结果
    struct Node{
        string str;
        int val;
    };
    
    bool nodeCmp(Node& t1, Node& t2){
        return t1.val == t2.val ? t1.str < t2.str : t1.val > t2.val;
    }
    
    int main()
    {
        // 获取考生信息
        int n, m;  // 输入和查询的数量
        cin >> n >> m;
        vector<Node> testees(n);
        for (int i = 0; i < n; i++)
            cin >> testees[i].str >> testees[i].val;
    
        // 获取要求并求解
        int type;
        string term;
        for (int caseIndex = 1; caseIndex <= m; caseIndex++){
            // 获取要求
            cin >> type >> term;
            printf("Case %d: %d %s
    ", caseIndex, type, term.c_str());
    
            // type为1和3时的结果
            vector<Node> results;
            switch (type){
                case 1 : {
                    for ( int i = 0; i < n; i++)
                        if (testees[i].str[0] == term[0])
                            results.push_back(testees[i]);
                    break;
                }
                case 2 : {
                    int testeeNum = 0, testeeScoreSum = 0;
                    for (int i = 0; i < n; i++)
                        if (testees[i].str.substr(1, 3) == term){
                            testeeNum += 1;
                            testeeScoreSum += testees[i].val;
                        }
                    if (testeeNum == 0 && testeeScoreSum == 0)
                        printf("NA
    ");
                    else
                        printf("%d %d
    ", testeeNum, testeeScoreSum);
                    break;
                }
                case 3 : {
                    unordered_map<string, int> statistics;
                    for (int i = 0; i < n; i++)
                        if (testees[i].str.substr(4, 6) == term)
                            statistics[testees[i].str.substr(1, 3)] += 1;
                    for (auto it : statistics)
                        results.push_back({it.first, it.second});
                }
            }
    
            if (type == 1 || type == 3){
                if (results.empty())
                    printf("NA
    ");
                else{
                    sort(results.begin(), results.end(), nodeCmp);
                    for (int i = 0; i < results.size(); i++)
                        printf("%s %d
    ", results[i].str.c_str(), results[i].val);
                }
            }
        }
        return 0;
    }
    

    作者:@臭咸鱼

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

    欢迎讨论和交流!


  • 相关阅读:
    关于排列组合与分配问题
    Stirling数
    UVA 294 求约数的个数
    Linux中profile与bashrc的作用
    一致性哈希(consistent hashing)算法
    TCP三次握手与四次挥手
    MySQL查询昨天、今天、7天、近30天、本月、上一月数据
    java基础-注解Annotation原理和用法
    java基础-浅复制与深复制的理解
    Linux命令行提示符设置
  • 原文地址:https://www.cnblogs.com/chouxianyu/p/13472524.html
Copyright © 2011-2022 走看看