【1153】Decode Registration Card of PAT (25分)

简介: 【1153】Decode Registration Card of PAT (25分)【1153】Decode Registration Card of PAT (25分)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<string>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
#include<unordered_map>
using namespace std;  
把map的key&value存到vector中,用sort对vector排序
注意c_str()可使printf用%s输出string类型字符串,为一指针
//编号规则,第1位代表pat等级,第2-4位为考场编号
//第5-10位为测试日期,第11-13位为测试者编号
struct node{
  string t;
  int value;
};
bool cmp(const node &a, const node &b){
  return a.value !=b.value ? a.value >b.value : a.t<b.t;
  //成绩不等则按成绩降序,成绩相等则按编号"升序"
}
int main(){   
  int n,k,num;
  string s;
  cin>>n>>k; //n个学生的数据,k次查询操作
  vector<node>v(n); //n个vector存储学生信息
  for(int i=0;i<n;i++){
    cin>>v[i].t>>v[i].value;//t存储对应学生编号,value存pat分数
  }
  for(int i=1;i<=k;i++){
    cin >>num>>s;//num为操作功能项,s为操作的字符
    printf("Case %d: %d %s\n",i,num,s.c_str());
    //输出第i个case:功能项,输入的字符串
    vector<node> ans;
    int cnt=0,sum=0;
    if(num==1){ 
      //第1个功能:输入考试等级,找出该等级的考生,按照成绩降序,准考证升序排序
      //按照等级查询,枚举匹配的学生然后排序
      for(int j=0;j<n;j++)
        if(v[j].t[0]==s[0]) 
          ans.push_back(v[j]);
    }else if(num==2){ 
      //第2个功能:输入考场号,统计该考场的考生数和总得分
      //按照考场查询,枚举匹配学生然后计数求和
      for(int j=0;j<n;j++){
        if(v[j].t.substr(1,3) == s){//截取出考场编号判断
          cnt++;
          sum += v[j].value;
        }
      }
      if(cnt!=0)  printf("%d %d\n",cnt,sum);
      //打印出改考场的  考生数 总分
    }else if(num==3) {
        //第3个功能:输入考试日期,查询该日期下所有考场的人数,按照人数降序,考场号升序
        //按日期查询每个考场人数,用unordered_map存储,再排序汇总
      unordered_map<string,int> m;
      for(int j=0;j<n;j++)
        if(v[j].t.substr(4,6)==s)  //找符合日期的考场
          m[v[j].t.substr(1,3)]++;//考场编号对应数加1
      for(auto it:m) {
        node t;//这里写得和柳神代码不同,可能是编译器用2012VS太老的原因之前那个会报出
        t.t=it.first;
        t.value=it.second;
        ans.push_back(t);
      }
    }
    //把map的key&value存到vector中,用sort对vector排序
    //m是map,ans是vector
    sort(ans.begin(),ans.end(),cmp); //排序
    for(int j=0;j<ans.size();j++)
      printf("%s %d\n",ans[j].t.c_str(),ans[j].value);
        //输出考场编号和总人数
    if ( (  (num==1 || num==3)&&ans.size()==0 )|| (num==2&&cnt==0) )
      printf("NA\n");
  }
  system("pause");
  return 0;
}
相关文章
|
9月前
|
JSON JavaScript API
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 错误分析
本文探讨了Python中处理JSON数据时遇到的`JSONDecodeError`,该错误通常由JSON格式错误或数据源问题引起。解决方法包括检查数据源、使用异常处理机制和调试日志记录。示例代码展示了如何从文件和API读取JSON并处理异常。注意事项涉及验证JSON规范、处理特殊字符和选择合适解析器。通过这些步骤,可以有效解决JSON解码错误,确保数据正确解析。
918 0
|
存储 C++ Windows
【PAT甲级 - C++题解】1153 Decode Registration Card of PAT
【PAT甲级 - C++题解】1153 Decode Registration Card of PAT
99 0
|
编解码
解码错误。‘gb2312‘ codec can‘t decode byte 0xf3 in position 307307: illegal multibyte sequence
解码错误。‘gb2312‘ codec can‘t decode byte 0xf3 in position 307307: illegal multibyte sequence
198 0
|
XML 网络协议 网络安全
base -2 Number——进制转换
题目描述 Given an integer N, find the base −2 representation of N. Here, S is the base −2 representation of N when the following are all satisfied: S is a string consisting of 0 and 1. Unless S= 0, the initial character of S is 1. Let S=SkSk−1…S0, then S0×(−2)0+S1×(−2)1+…+Sk×(−2)k=N.
131 0
【1082】Read Number in Chinese (25 分)
【1082】Read Number in Chinese (25 分) 【1082】Read Number in Chinese (25 分)
121 0
|
存储 缓存 固态存储
Long Story of Block - 1 Data Unit
计算、存储、网络构成了云计算的基本组件。Linux 中的 IO 栈主要分为 Filesystem 与 Block 两层,前者包括 VFS 与各种类型的文件系统(包括 Ext4、XFS 等),描述了数据的组织形式、提供管理数据的接口;而后者包括通用块层 (generic block layer) 与各种类型的块设备驱动(包括 SCSI、NVMe、Virtio 等),主要实现了数据在非易失性存储(HD
435 1
Long Story of Block - 1 Data Unit
|
Web App开发 算法
名词小结:base href、GreaseMonkey、Varchar、char、网速的计算
base href、GreaseMonkey、Varchar、char、网速的计算
1225 0
|
Web App开发 关系型数据库 Java
Data truncation: Data too long for column 'xxx' at row 1
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/78870542 ...
2160 0
|
Web App开发 Java 关系型数据库
Data truncation: Data too long for column &#39;xxx&#39; at row 1
Data truncation: Data too long for column 'xxx' at row 1 完整的错误内容可能是下面这样的: p.p1 {margin: 0.0px 0.0px 0.
2258 0