【PAT乙级】解码PAT准考证

  • 2019 年 11 月 8 日
  • 筆記

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/85017022

题目描述:

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

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

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

输入描述:

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

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

考生信息之后,再给出 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

解题思路:

这题我在考试的时候写了一个小时才写出来,提交之后只有测试点0AC,测试点1、2WA,测试点3、4TLE。我知道WA和TLE的问题都是因为类别三排序和输出有bug。类别三是要在考场人数降序的基础上将考场编号升序输出,我一开始用了map,结果不知道map怎么先按value值大小降序,当value值相等时再按key值大小升序排序。于是我就用了个数组+双重for循环来操作,果不其然TLE。然后我在考场就开始了长达半个小时的debug,结果该WA的还是WA,该TLE的还是TLE,时间还白白浪费掉了。今晚上我又花了一个小时来不停地debug,终于发现造成TLE的原因:①cout和stdout的同步,导致超时;②比较函数传递参数的时候引用传参要比较快。下面三张截图比较一下19分 22分 25分的运行时间。

19分,后俩个测试直接TLE。

22分,测试点4耗时195msAC,测试点3依旧TLE。

25分,测试点4耗时从195ms边成了69ms,测试点3耗时147msAC。

15分代码:

测试点0AC,测试点1、2WA,测试点3、4TLE。

#include <bits/stdc++.h>  using namespace std;    struct stu  {      string ID;   //准考证号      int score;   //分数  };    bool Cmp(stu a,stu b)  {      //lambda表达式,先按分数降序排列,若分数相等则按准考证号升序排列      return a.score!=b.score? a.score>b.score : a.ID<b.ID;  }    int main()  {      int N,M;      ios::sync_with_stdio(false);   //取消cin和stdin的同步      cin >> N >> M;      vector<stu> v(N);      for (int i = 0; i < N; i++)      {          cin >> v[i].ID >> v[i].score;      }      int xh = 0;  //序号      while(M--)      {          xh++;          bool flag = false;   //false时输出NA          int lx;     //类型          cin >> lx;          if(lx == 1)          //类型1要求按分数非升序输出某个指定级别的考生的成绩          {              char zl;              cin >> zl;              cout << "Case " << xh << ": " << lx << " " << zl << endl;              sort(v.begin(),v.end(),Cmp);              for (auto it = v.begin(); it != v.end(); it++)              {                  if(it->ID[0] == zl)                  {                      flag = true;                      cout << it->ID << " " << it->score << endl;                  }              }          }          else if(lx == 2)          //类型2要求将某指定考场的考生人数和总分统计输出          {              int zl;              cin >> zl;              cout << "Case " << xh << ": " << lx << " " << zl << endl;              int count_r = 0,count_f=0;  //人 分              for (auto it = v.begin(); it != v.end(); it++)              {                  string tempstr = "";                  for (int i = 1; i <= 3; i++)                  {                      tempstr += it->ID[i];                  }                  int temp = atoi(tempstr.c_str());                  if(temp == zl)                  {                      count_r++;                      count_f += it->score;                      flag = true;                  }              }              if(!flag)    //查询结果为空              {                  cout << "NA" << endl;              }              else              {                  cout << count_r << " " << count_f << endl;              }          }          else if(lx == 3)          //类型3要求将某指定考场的考生人数和总分统计输出          {              string zl;              cin >> zl;              cout << "Case " << xh << ": " << lx << " " << zl << endl;              int a[1000];              memset(a,0,sizeof(a));              for (auto it = v.begin(); it != v.end(); it++)              {                  string tempdata = "";  //日期                  for (int i = 4;i <= 9; i++)                  {                      tempdata += it->ID[i];                  }                  if(tempdata == zl)                  {                      flag = true;                      string tempstr = "";  //考场                      for(int i=1;i<=3;i++)                      {                          tempstr += it->ID[i];                      }                      int temp = atoi(tempstr.c_str());                      a[temp]++;                  }              }              if(!flag)              {                  cout << "NA" << endl;              }              else              {                  for (int i = 100; i > 0; i--)                  {                      for (int j = 100; j < 1000; j++)                      {                          if(a[j] == i)                          cout << j << " " << a[j] << endl;                      }                  }              }            }      }      return 0;  }

19分代码:

测试点3、4依旧TLE。跟15分的代码区别:①在类别1中加入了一个if(!flag)输出NA的语句;②把类别3的数组换成了vector+map。

#include <bits/stdc++.h>  using namespace std;    struct stu   //考生  {      string ID;   //准考证号      int score;   //分数  };    struct room   //考场  {      string ID;   //考场编号      int count;    //考场人数  };    bool Cmp1(stu a,stu b)   //类型1用到的比较方法  {      //lambda表达式,先按分数降序排列,若分数相等则按准考证号升序排列      return a.score!=b.score? a.score>b.score : a.ID<b.ID;  }    bool Cmp3(room a,room b)   //类型3用到的比较方法  {      //lambda表达式,先按考场人数降序排列,若考场人数相同则按考场标号升序排列      return a.count!=b.count? a.count>b.count : a.ID<b.ID;  }    int main()  {      int N,M;      ios::sync_with_stdio(false);   //取消cin和stdin的同步      cin >> N >> M;      vector<stu> v(N);      for (int i = 0; i < N; i++)      {          cin >> v[i].ID >> v[i].score;      }      int xh = 0;  //序号      while(M--)      {          xh++;          bool flag = false;   //false时输出NA          int lx;     //类型          cin >> lx;          if(lx == 1)          //类型1要求按分数非升序输出某个指定级别的考生的成绩          {              char zl;              cin >> zl;              cout << "Case " << xh << ": " << lx << " " << zl << endl;              sort(v.begin(),v.end(),Cmp1);              for (auto it = v.begin(); it != v.end(); it++)              {                  if(it->ID[0] == zl)                  {                      flag = true;                      cout << it->ID << " " << it->score << endl;                  }              }              if(!flag)              {                  cout << "NA" << endl;              }          }          else if(lx == 2)          //类型2要求将某指定考场的考生人数和总分统计输出          {              int zl;              cin >> zl;              cout << "Case " << xh << ": " << lx << " " << zl << endl;              int count_r = 0,count_f=0;  //人 分              for (auto it = v.begin(); it != v.end(); it++)              {                  string tempstr = it->ID.substr(1,3);                  int temp = atoi(tempstr.c_str());                  if(temp == zl)                  {                      count_r++;                      count_f += it->score;                      flag = true;                  }              }              if(!flag)    //查询结果为空              {                  cout << "NA" << endl;              }              else              {                  cout << count_r << " " << count_f << endl;              }          }          else if(lx == 3)          //类型3要求将某指定考场的考生人数和总分统计输出          {              string zl;              cin >> zl;              cout << "Case " << xh << ": " << lx << " " << zl << endl;              vector<room> kc;              map<string,int> m;              for (auto it = v.begin(); it != v.end(); it++)              {                  if(it->ID.substr(4,6) == zl)                  {                      flag = true;                      string tempstr = it->ID.substr(1,3);  //考场                      //int temp = atoi(tempstr.c_str());                      m[tempstr]++;                  }              }              for (auto it : m)   //把map全部推入vector中来排序              {                  kc.push_back({it.first,it.second});              }              sort(kc.begin(),kc.end(),Cmp3);              if(!flag)              {                  cout << "NA" << endl;              }              else              {                  for (int i = 0; i < kc.size(); i++)                  {                      cout << kc[i].ID  << " " << kc[i].count << endl;                  }              }          }      }      return 0;  }

22分代码:

测试点3TLE。跟19分代码的区别:看了大佬的代码,把排序函数的传参数改成了引用传参,她说这样更快。但是依旧有测试用例TLE。

#include <bits/stdc++.h>  using namespace std;    struct stu   //考生  {      string ID;   //准考证号      int score;   //分数  };    struct room   //考场  {      string ID;   //考场编号      int count;    //考场人数  };    bool Cmp1(stu &a,stu &b)   //类型1用到的比较方法  {      //lambda表达式,先按分数降序排列,若分数相等则按准考证号升序排列      return a.score!=b.score? a.score>b.score : a.ID<b.ID;  }    bool Cmp3(room &a,room &b)   //类型3用到的比较方法  {      //lambda表达式,先按考场人数降序排列,若考场人数相同则按考场标号升序排列      return a.count!=b.count? a.count>b.count : a.ID<b.ID;  }    int main()  {      int N,M;      ios::sync_with_stdio(false);   //取消cin和stdin的同步      cin >> N >> M;      vector<stu> v(N);      for (int i = 0; i < N; i++)      {          cin >> v[i].ID >> v[i].score;      }      int xh = 0;  //序号      while(M--)      {          xh++;          bool flag = false;   //false时输出NA          int lx;     //类型          cin >> lx;          if(lx == 1)          //类型1要求按分数非升序输出某个指定级别的考生的成绩          {              char zl;              cin >> zl;              cout << "Case " << xh << ": " << lx << " " << zl << endl;              sort(v.begin(),v.end(),Cmp1);              for (auto it = v.begin(); it != v.end(); it++)              {                  if(it->ID[0] == zl)                  {                      flag = true;                      cout << it->ID << " " << it->score << endl;                  }              }              if(!flag)              {                  cout << "NA" << endl;              }          }          else if(lx == 2)          //类型2要求将某指定考场的考生人数和总分统计输出          {              int zl;              cin >> zl;              cout << "Case " << xh << ": " << lx << " " << zl << endl;              int count_r = 0,count_f=0;  //人 分              for (auto it = v.begin(); it != v.end(); it++)              {                  string tempstr = it->ID.substr(1,3);                  int temp = atoi(tempstr.c_str());                  if(temp == zl)                  {                      count_r++;                      count_f += it->score;                      flag = true;                  }              }              if(!flag)    //查询结果为空              {                  cout << "NA" << endl;              }              else              {                  cout << count_r << " " << count_f << endl;              }          }          else if(lx == 3)          //类型3要求将某指定考场的考生人数和总分统计输出          {              string zl;              cin >> zl;              cout << "Case " << xh << ": " << lx << " " << zl << endl;              vector<room> kc;              map<string,int> m;              for (auto it = v.begin(); it != v.end(); it++)              {                  if(it->ID.substr(4,6) == zl)                  {                      flag = true;                      string tempstr = it->ID.substr(1,3);  //考场                      //int temp = atoi(tempstr.c_str());                      m[tempstr]++;                  }              }              for (auto it : m)   //把map全部推入vector中来排序              {                  kc.push_back({it.first,it.second});              }              sort(kc.begin(),kc.end(),Cmp3);              if(!flag)              {                  cout << "NA" << endl;              }              else              {                  for (int i = 0; i < kc.size(); i++)                  {                      cout << kc[i].ID  << " " << kc[i].count << endl;                  }              }          }      }      return 0;  }

25分代码:

跟22分代码的区别:①把所有的cout语句换成了printf,因为cout和stdout保持同步导致速度很慢,又没有类似cin和stdin的取消同步语句ios::sync_with_stdio(false)。需要注意的是printf里的%s是不能直接输出string型的,必须先用c_str()转换成*char型。

#include <bits/stdc++.h>  using namespace std;    struct stu   //考生  {      string ID;   //准考证号      int score;   //分数  };    struct room   //考场  {      string ID;   //考场编号      int count;    //考场人数  };    bool Cmp1(stu &a,stu &b)   //类型1用到的比较方法  {      //lambda表达式,先按分数降序排列,若分数相等则按准考证号升序排列      return a.score!=b.score? a.score>b.score : a.ID<b.ID;  }    bool Cmp3(room &a,room &b)   //类型3用到的比较方法  {      //lambda表达式,先按考场人数降序排列,若考场人数相同则按考场标号升序排列      return a.count!=b.count? a.count>b.count : a.ID<b.ID;  }    int main()  {      int N,M;      ios::sync_with_stdio(false);   //取消cin和stdin的同步      cin >> N >> M;      vector<stu> v(N);      for (int i = 0; i < N; i++)      {          cin >> v[i].ID >> v[i].score;      }      int xh = 0;  //序号      while(M--)      {          xh++;          bool flag = false;   //false时输出NA          int lx;     //类型          cin >> lx;          if(lx == 1)          //类型1要求按分数非升序输出某个指定级别的考生的成绩          {              char zl;              cin >> zl;              printf("Case %d: %d %cn", xh, lx, zl);              sort(v.begin(),v.end(),Cmp1);              for (auto it = v.begin(); it != v.end(); it++)              {                  if(it->ID[0] == zl)                  {                      flag = true;                      printf("%s %dn", it->ID.c_str(), it->score);                  }              }              if(!flag)              {                  printf("NAn");              }          }          else if(lx == 2)          //类型2要求将某指定考场的考生人数和总分统计输出          {              int zl;              cin >> zl;              printf("Case %d: %d %dn", xh, lx, zl);              int count_r = 0,count_f=0;  //人 分              for (auto it = v.begin(); it != v.end(); it++)              {                  string tempstr = it->ID.substr(1,3);                  int temp = atoi(tempstr.c_str());                  if(temp == zl)                  {                      count_r++;                      count_f += it->score;                      flag = true;                  }              }              if(!flag)    //查询结果为空              {                  printf("NAn");              }              else              {                  printf("%d %dn", count_r, count_f);              }          }          else if(lx == 3)          //类型3要求将某指定考场的考生人数和总分统计输出          {              string zl;              cin >> zl;              printf("Case %d: %d %sn", xh, lx, zl.c_str());              vector<room> kc;              unordered_map<string,int> m;              for (auto it = v.begin(); it != v.end(); it++)              {                  if(it->ID.substr(4,6) == zl)                  {                      flag = true;                      string tempstr = it->ID.substr(1,3);  //考场                      //int temp = atoi(tempstr.c_str());                      m[tempstr]++;                  }              }              for (auto it : m)   //把map全部推入vector中来排序              {                  kc.push_back({it.first,it.second});              }              sort(kc.begin(),kc.end(),Cmp3);              if(!flag)              {                  printf("NAn");              }              else              {                  for (int i = 0; i < kc.size(); i++)                  {                      printf("%s %dn", kc[i].ID.c_str(), kc[i].count);                  }              }          }      }      return 0;  }