PAT Basic 1095 解码PAT准考证 (25 分)

PAT 准考证号由 4 部分组成:

  • 第 1 位是级别,即 T 代表顶级;A 代表甲级;B 代表乙级;
  • 第 2~4 位是考场编号,范围从 101 到 999;
  • 第 5~10 位是考试日期,格式为年、月、日顺次各占 2 位;
  • 最后 11~13 位是考生编号,范围从 000 到 999。

现给定一系列考生的准考证号和他们的成绩,请你按照要求输出各种统计信息。

输入格式:

输入首先在一行中给出两个正整数 N(≤)和 M(≤),分别为考生人数和统计要求的个数。

接下来 N 行,每行给出一个考生的准考证号和其分数(在区间 [ 内的整数),其间以空格分隔。

考生信息之后,再给出 M 行,每行给出一个统计要求,格式为:类型 指令,其中

  • 类型 为 1 表示要求按分数非升序输出某个指定级别的考生的成绩,对应的 指令 则给出代表指定级别的字母;
  • 类型 为 2 表示要求将某指定考场的考生人数和总分统计输出,对应的 指令 则给出指定考场的编号;
  • 类型 为 3 表示要求将某指定日期的考生人数分考场统计输出,对应的 指令 则给出指定日期,格式与准考证上日期相同。

输出格式:

对每项统计要求,首先在一行中输出 Case #: 要求,其中 # 是该项要求的编号,从 1 开始;要求 即复制输入给出的要求。随后输出相应的统计结果:

  • 类型 为 1 的指令,输出格式与输入的考生信息格式相同,即 准考证号 成绩。对于分数并列的考生,按其准考证号的字典序递增输出(题目保证无重复准考证号);
  • 类型 为 2 的指令,按 人数 总分 的格式输出;
  • 类型 为 3 的指令,输出按人数非递增顺序,格式为 考场编号 总人数。若人数并列则按考场编号递增顺序输出。

如果查询结果为空,则输出 NA

输入样例:

8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999

输出样例:

Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA

作者: 陈越

单位: 浙江大学

时间限制: 200 ms

内存限制: 64 MB



#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct stu{
    string num;
    int grade;
};
bool cmp1(const stu& s1,const stu& s2){
    if(s1.grade!=s2.grade) return s1.grade>s2.grade;
    else return s1.num<s2.num;
}
bool cmp3(const pair<string,int>& p1,const pair<string,int>& p2){
    if(p1.second!=p2.second) return p1.second>p2.second;
    else return p1.first<p2.first;
}
int main()
{
    int peo,test;stu tmp;
    int case_num;string case_str;
    cin>>peo>>test;
    vector<stu> vec;
    for(int i=0;i<peo;i++){
        cin>>tmp.num>>tmp.grade;
        vec.push_back(tmp);
    }
    for(int i=1;i<=test;i++){
        cin>>case_num>>case_str;
        printf("Case %d: %d %s\n",i,case_num,case_str.data());
        if(case_num==1){
            vector<stu> vec1;
            for(int j=0;j<peo;j++){
                if(vec[j].num[0]==case_str[0]) vec1.push_back(vec[j]);
            }
            sort(vec1.begin(),vec1.end(),cmp1);
            for(int j=0;j<vec1.size();j++)
                printf("%s %d\n",vec1[j].num.data(),vec1[j].grade);
            if(vec1.size()==0) printf("NA\n");
        }else if(case_num==2){
            int num=0,score=0;
            for(int j=0;j<peo;j++){
                if(vec[j].num.substr(1,3)==case_str){
                    num++;score+=vec[j].grade;
                }
            }
            if(num==0) printf("NA\n");
            else printf("%d %d\n",num,score);
        }else{
            unordered_map<string,int> m;
            for(int j=0;j<peo;j++){
                if(vec[j].num.substr(4,6)==case_str){
                    m[vec[j].num.substr(1,3)]++;
                }
            }
            vector<pair<string,int>> vec3(m.begin(),m.end());
            sort(vec3.begin(),vec3.end(),cmp3);
            for(int i=0;i<vec3.size();i++)
                printf("%s %d\n",vec3[i].first.data(),vec3[i].second);
            if(vec3.size()==0) printf("NA\n");
        }
    }
    system("pause");
    return 0;
}

我这边乙级甲级出现了同样的错误,就是这个NA,应该每个都应该打印。

超时,使用unordered_map,如果还是超时,那么把cout换成printf,如果还是超时,那么把cin换成scanf

原文地址:https://www.cnblogs.com/littlepage/p/11617828.html

时间: 2024-08-30 08:15:01

PAT Basic 1095 解码PAT准考证 (25 分)的相关文章

PAT Basic 1075 链表元素分类 (25 分)

给定一个单链表,请编写程序将链表元素进行分类排列,使得所有负值元素都排在非负值元素的前面,而 [0, K] 区间内的元素都排在大于 K 的元素前面.但每一类内部元素的顺序是不能改变的.例如:给定链表为 18→7→-4→0→5→-6→10→11→-2,K 为 10,则输出应该为 -4→-6→-2→7→0→5→10→18→11. 输入格式: 每个输入包含一个测试用例.每个测试用例第 1 行给出:第 1 个结点的地址:结点总个数,即正整数N (≤):以及正整数K (≤).结点的地址是 5 位非负整数,

PTA乙级 (*1095 解码PAT准考证 (25分))

1095 解码PAT准考证 (25分) https://pintia.cn/problem-sets/994805260223102976/problems/1071786104348536832 题目大意:给出一组学生的准考证号和成绩,准考证号包含了等级(乙甲顶),考场号,日期,和个人编号信息,并有三种查询方式查询一:给出考试等级,找出该等级的考生,按照成绩降序,准考证升序排序查询二:给出考场号,统计该考场的考生数量和总得分查询三:给出考试日期,查询改日期下所有考场的考试人数,按照人数降序,考

PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. In

pat 1013 Battle Over Cities(25 分) (并查集)

1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any othe

pat 1149 Dangerous Goods Packaging(25 分)

1149 Dangerous Goods Packaging(25 分) When shipping goods with containers, we have to be careful not to pack some incompatible goods into the same container, or we might get ourselves in serious trouble. For example, oxidizing agent (氧化剂) must not be

PAT 甲级 1083 List Grades (25 分)

1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval. I

PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any oth

PAT 甲级 1036 Boys vs Girls (25 分)(简单题)

1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contai

[PTA] PAT(A) 1007 Maximum Subsequence Sum (25 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Problem portal: 1007 Maximum Subsequence Sum (25 分) Description Given a sequence of $K$ integers { $N_{1}?$, $N_{2}?$, $...$, $N_{K}$ }. A continuous subsequ