PAT甲级1153 Decode Registration Card of PAT (25分)|C++实现

一、题目描述

原题链接
在这里插入图片描述

Input Specification:

在这里插入图片描述

​​Output Specification:

在这里插入图片描述

Sample Input:

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

Sample Output:

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

二、解题思路

一道比较复杂的题目,我这里是把所有的query都分别封装了对应的函数进行处理,各种信息的获取我们用了string中的substr函数。详情见代码注释。

三、AC代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;
const int maxs = 1010;
const int maxd = 100010;
unordered_map<int, int> mp; //存放每个考场的考生数
struct Testee   //考生
{
    
    
  string id;
  int score;
};
struct Site //考场
{
    
    
  int id, cnt=0, total=0, cntD = 0;
  string date;
}site[maxs];
vector<Testee> A, B, T, all;    //存放甲级乙级顶级,以及所有的考生
vector<Site> ans;   //存放结果考场
bool cmp1(Testee a, Testee b)
{
    
    
  if(a.score != b.score)	return a.score > b.score;   //按分数排序
  else	return a.id.compare(b.id) < 0;  //按id号
}
//bool cmp2(Site a, Site b)   //考场按人数排序,人数相同按照考场号排序
//{
    
    
//  if(a.cnt!=b.cnt)	return a.cnt > b.cnt;
//  else	return a.id < b.id;
//}
bool cmp3(int a, int b) //考场按人数排序,人数相同按照考场号排序
{
    
    
    return mp[a]!=mp[b]? mp[a]>mp[b] : a<b;
}
void printV(vector<Testee> v)   //输出函数
{
    
    
  for(int i=0; i<v.size(); i++)
    printf("%s %d\n", v[i].id.c_str(), v[i].score);
}
void checkRank(string str)  //排名处理函数
{
    
    
  if(str == "T")
  {
    
    
    if(T.size() == 0)	printf("NA\n");
    else
    {
    
    
      sort(T.begin(), T.end(), cmp1);
      printV(T);
    }
  }
  else if(str == "A")
  {
    
    
    if(A.size() == 0)	printf("NA\n");
    else
    {
    
    
      sort(A.begin(), A.end(), cmp1);
    	printV(A);
    }
  }
  else
  {
    
    
    if(B.size() == 0)	printf("NA\n");
    else
    {
    
    
      sort(B.begin(), B.end(), cmp1);
      printV(B);
    }
  }
}
void checkSite(string str)  //考场处理函数
{
    
    
  int tmp = stoi(str);
  if(site[tmp].cnt == 0)
    printf("NA\n");
  else
	  printf("%d %d\n", site[tmp].cnt, site[tmp].total);
}
void checkDate(string str)  //日期处理函数
{
    
    
  vector<int> v;
  mp.clear();
  for(int i=0; i<all.size(); i++)
  {
    
    
      string d = all[i].id.substr(4, 6);
      int room = stoi(all[i].id.substr(1, 3));
      if(d == str)  mp[room]++; //对应教室人数++
  }
  for(auto it:mp)   //将考场号存入v中
  {
    
    
      v.push_back(it.first);
  }
  if(v.size() == 0)   printf("NA\n");
  sort(v.begin(), v.end(), cmp3);
  for(int i=0; i<v.size(); i++)
      printf("%03d %d\n", v[i], mp[v[i]]);
}
int main()
{
    
    
  int N, M;
  Testee tmp;
  scanf("%d%d", &N, &M);
  for(int i=0; i<N; i++)
  {
    
    
    cin >> tmp.id;
    scanf("%d", &tmp.score);
    all.push_back(tmp);
    if(tmp.id[0] == 'A')	A.push_back(tmp);
    else if(tmp.id[0] == 'B')	B.push_back(tmp);
    else	T.push_back(tmp);
    string room = tmp.id.substr(1, 3);
    int roomnum = stoi(room);
    site[roomnum].id = roomnum;
    site[roomnum].cnt++;
    site[roomnum].total += tmp.score;
  }
  int test;
  string str;
  for(int i=0; i<M; i++)
  {
    
    
    scanf("%d", &test);
    cin >> str;
    printf("Case %d: %d %s\n", i+1, test, str.c_str());
    if(test == 1)	checkRank(str); //执行对应操作
    else if(test == 2)	checkSite(str);
    else if(test == 3)	checkDate(str);
    else	printf("NA\n");
  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42393947/article/details/109126426