记第一次参加PAT(附18.12.09PAT乙级题解)


写在前面:

18年12月09日 第一次参加PAT乙级,在停止提交的那一刻,我的心里只有俩个字:遗憾。这次乙级的后俩题是甲级的前俩题一模一样,不过甲级题是英文。并不是输在了题目有多难,以我这个菜鸡的水平,这套乙级题应该至少能考85+的。可是由于我时间分配的不合理,导致了这次考试的失败。真是芝麻没捡到还弄丢了个大西瓜。考试时间总共是3个小时,前俩题全部AC我用了40分钟,还有2个小时10分钟才结束考试,然后心里就有点飘啊。看完第三题之后,我小声哔哔了一句:“这个题目也太水了吧。”然后我直接跳到了第4题写,因为第3题真的太水了。第四题20分得了18分用时30分钟,写完后还有1个小时40分钟结束考试。最后一题25分!写了我一个多小时提交代码之后只得了15分,然后我debug了半个多小时,最后10分钟才猛然发现第3道水题还没写,然后我就很慌张。其实考完后发现第三题就直接A+B,输出的时候用map来记录字符不要输出重复字符就行了。可是只剩最后10分钟我这个菜鸡慌了,在考场发慌的时候思路是凌乱的最后这道题是空白的,没有提交过一次。总结一下教训:以后考试不要飘!不要膨胀!不要跳过水题先写难题!不要慌!先把所有题都提交得分后再debug!

涉及到的一些知识:

①map的用法;②string型转char*型函数:c_str();③char*型转int型函数:atoi();④vector的用法。

题目列表: 

第5题万恶的TLE,我考完之后都debug了3个版本才全AC。这些题都可以点击跳转。

1. N-自守数 (15 分)

2. 最好吃的月饼 (20 分)

3. 字符串A+B (20 分)

4. 谷歌的招聘 (20 分)

5. 解码PAT准考证 (25 分)

题目解析:

1. N-自守数 (15 分)

题目描述:

如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”。例如 3×92​2​​=25392,而 25392 的末尾两位正好是 92,所以 92 是一个 3-自守数。

本题就请你编写程序判断一个给定的数字是否关于某个 N 是 N-自守数。

输入格式:

输入在第一行中给出正整数 M(≤20),随后一行给出 M 个待检测的、不超过 1000 的正整数。

输出格式:

对每个需要检测的数字,如果它是 N-自守数就在一行中输出最小的 N 和 NK​2​​ 的值,以一个空格隔开;否则输出 No。注意题目保证 N<10。

输入样例:

3
92 5 233

输出样例:

3 25392
1 25
No

解题思路:

这题我自定义了一个fun函数来计算该函数的形参n向上取整的len是10、100还是1000。用for循环在1~10中寻找那个能使(K*K*N)%len = K成立的N,要是找到了就用ans来记录这个N,跳出循环输出ans。要是for循环结束还没找到,就输出No。

AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 int fun(int n)  5 {  6     int count = 0;  7     while(n > 0)  8  {  9         count++; 10         n /= 10; 11  } 12     int sum = 1; 13     for (int i = 0; i < count; i++) 14  { 15         sum *= 10; 16  } 17     return sum; 18 } 19 
20 int main() 21 { 22     int M; 23     cin >> M; 24     while(M--) 25  { 26         int K; 27         cin >> K; 28         int len = fun(K); 29         //cout << len << endl;
30         int sum; 31         int ans = -1; 32         for (int i = 1; i < 10; i++) 33  { 34             sum = K*K*i; 35             if(sum%len == K) 36  { 37                 ans = i; 38                 break; 39  } 40  } 41         if(ans == -1) 42  { 43             cout << "No" << endl; 44  } 45         else
46  { 47             cout << ans << " " << sum << endl; 48  } 49  } 50     return 0; 51 }

2. 最好吃的月饼 (20 分)

题目描述:

月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种。

mk.jpg

若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给出全国各地各种月饼的销量,要求你从中找出销量冠军,认定为最好吃的月饼。

输入格式:

输入首先给出两个正整数 N(≤1000)和 M(≤100),分别为月饼的种类数(于是默认月饼种类从 1 到 N 编号)和参与统计的城市数量。

接下来 M 行,每行给出 N 个非负整数(均不超过 1 百万),其中第 i 个整数为第 i 种月饼的销量(块)。数字间以空格分隔。

输出格式:

在第一行中输出最大销量,第二行输出销量最大的月饼的种类编号。如果冠军不唯一,则按编号递增顺序输出并列冠军。数字间以 1 个空格分隔,行首尾不得有多余空格。

输入样例:

5 3
1001 992 0 233 6
8 0 2018 0 2008
36 18 0 1024 4

输出样例:

2018
3 5

解题思路:

先建立一个map,map的key值是月饼编号,value值是该月饼的销量,用ans来记录最大销量。创建一个冠军数组a用来标记冠军销量的月饼编号。若某种月饼的销量大于冠军月饼的销量,则把数组a置空重新标记冠军月饼的编号。最后for循环遍历数组a输出值为1的所在下标。

AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 int main()  5 {  6     map<int,int> m;   //月饼的编号为key,销量为value
 7     int N,M;  8     cin >> N >> M;  9     int ans = 0;   //最大销量
10     int a[1005];    //存放冠军,1为冠军,0非冠军
11     memset(a,0,sizeof(a));   //全部初始化为0
12     while(M--) 13  { 14         for(int i=1;i<=N;i++) 15  { 16             int temp; 17             cin >> temp; 18             m[i] += temp; 19             if(m[i] > ans)   //若销量大于冠军
20  { 21                 ans = max(ans,m[i]);    //这里可以写ans = m[i];
22                 memset(a,0,sizeof(a));   //a清零
23                 a[i] = 1; 24  } 25             else if(m[i] == ans)  //若销量等于冠军
26  { 27                 if(a[i] == 0)   //判断是否已经进入冠军数组
28  { 29                     a[i] = 1;   //要是没进冠军数组就标记一下
30  } 31  } 32  } 33  } 34     cout << ans << endl; 35     bool flag = true;    //为了输出格式而立的flag
36     for ( int i = 1; i < 1005; i++) 37  { 38         if(a[i] != 0) 39  { 40             if(flag) 41  { 42                 flag = false; 43                 cout << i; 44  } 45             else
46  { 47                 cout << " " << i; 48  } 49  } 50  } 51     return 0; 52 }

3. 字符串A+B (20 分)

题目描述:

给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集。要求先输出 A,再输出 B,但重复的字符必须被剔除。

输入格式:

输入在两行中分别给出 A 和 B,均为长度不超过 10​6​​的、由可见 ASCII 字符 (即码值为32~126)和空格组成的、由回车标识结束的非空字符串。

输出格式:

在一行中输出题面要求的 A 和 B 的和。

输入样例:

This is a sample test
to show you_How it works

输出样例:

This ampletowyu_Hrk

解题思路:

这个题真的不难,可是这一次PAT乙级考试,我的的确确就输在了这题上面,在考试的时候由于我时间分配的不合理,我这题没提交过一次代码,20分直接没了。5道题里我就空了这一道水题没提交过、没有得分。停止提交之后的5分钟,我写出了代码。这题不就是用map来记录输出过的字符,不重复地输出字符就AC了吗?可以说是非常遗憾了。

AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     string a,b;
 7     getline(cin,a);
 8     getline(cin,b);
 9     string result = a+b;
10     map<char,int> m;    //map的key值是字符,value值用0和1来记录是否输出过
11     for(auto it: result)  //for-each循环遍历字符串A+B
12     {
13         if(m[it]==0)    //若该字符没有出现过
14         {
15             cout << it;  //输出该字符
16             m[it] = 1;   //并在map中记录它输出过了
17         }
18     }
19     return 0;
20 }

4. 谷歌的招聘 (20 分)

题目描述:

2004 年 7 月,谷歌在硅谷的 101 号公路边竖立了一块巨大的广告牌(如下图)用于招聘。内容超级简单,就是一个以 .com 结尾的网址,而前面的网址是一个 10 位素数,这个素数是自然常数 e 中最早出现的 10 位连续数字。能找出这个素数的人,就可以通过访问谷歌的这个网站进入招聘流程的下一步。

prime.jpg

自然常数 e 是一个著名的超越数,前面若干位写出来是这样的:e = 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921... 其中粗体标出的 10 位数就是答案。

本题要求你编程解决一个更通用的问题:从任一给定的长度为 L 的数字中,找出最早出现的 K 位连续数字所组成的素数。

输入格式:

输入在第一行给出 2 个正整数,分别是 L(不超过 1000 的正整数,为数字长度)和 K(小于 10 的正整数)。接下来一行给出一个长度为 L 的正整数 N。

输出格式:

在一行中输出 N 中最早出现的 K 位连续数字所组成的素数。如果这样的素数不存在,则输出 404。注意,原始数字中的前导零也计算在位数之内。例如在 200236 中找 4 位素数,0023 算是解;但第一位 2 不能被当成 0002 输出,因为在原始数字中不存在这个 2 的前导零。

输入样例 1:

20 5
23654987725541023819

输出样例 1:

49877

输入样例 2:

10 3
2468024680

输出样例 2:

404

解题思路:

我在考试过程中提交的代码只有18分,6个测试用例中3和4WA了。当时觉得第3题太水,直接跳第4题写的(错误的决定)。遍历每个K位string型数字,先用c_str()函数强制转换成char*型,再用atoi()函数强制转换成int型判断它是不是素数。若是素数,把该string型的K位数字赋值给result进行输出,否则输出result的初始化字符串404。

18分代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 bool isPrime(int n)  5 {  6     if(n <= 1)  7  {  8         return false;  9  } 10     for(int i =2;i<=sqrt(n);i++) 11  { 12         if(n%i == 0) 13  { 14             return false; 15  } 16  } 17     return true; 18 } 19 
20 int main() 21 { 22     int L,K; 23     string N; 24     cin >> L >> K >> N; 25     string result = "404"; 26     for (int i = 0; i < L; i++) 27  { 28         int j = i; 29         string tempstr = ""; 30         for (int j = i; j < i+K; j++) 31  { 32             tempstr += N[j];  //K个连续的数字
33  } 34         int temp = atoi(tempstr.c_str());  //string型的数字先转成char*再转成int型的数字
35         if(isPrime(temp))  //若这个数字是素数,则用result来记录它
36  { 37             result = tempstr; 38             break; 39  } 40  } 41     cout << result << endl; 42     return 0; 43 }

AC代码:

考试结束之后发现,把上面的18分代码改成用substr()来得到K位的字符子串就能通过测试用例3、4啦。

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 bool isPrime(int n)  5 {  6     if(n <= 1)  7  {  8         return false;  9  } 10     for(int i =2;i<=sqrt(n);i++) 11  { 12         if(n%i == 0) 13  { 14             return false; 15  } 16  } 17     return true; 18 } 19 
20 int main() 21 { 22     int L,K; 23     string N; 24     cin >> L >> K >> N; 25     string result = "404"; 26     for (int i = 0; i <= L-K; i++) 27  { 28         string tempstr = N.substr(i,K); 29         int temp = atoi(tempstr.c_str());  //string型的数字先转成char*再转成int型的数字
30         if(isPrime(temp))  //若这个数字是素数,则用result来记录它
31  { 32             result = tempstr; 33             break; 34  } 35  } 36     cout << result << endl; 37     return 0; 38 }

5. 解码PAT准考证 (25 分)

题目描述:

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。

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 struct stu  5 {  6     string ID;   //准考证号
 7     int score;   //分数
 8 };  9 
 10 bool Cmp(stu a,stu b)  11 {  12     //lambda表达式,先按分数降序排列,若分数相等则按准考证号升序排列
 13     return a.score!=b.score? a.score>b.score : a.ID<b.ID;  14 }  15 
 16 int main()  17 {  18     int N,M;  19     ios::sync_with_stdio(false);   //取消cin和stdin的同步
 20     cin >> N >> M;  21     vector<stu> v(N);  22     for (int i = 0; i < N; i++)  23  {  24         cin >> v[i].ID >> v[i].score;  25  }  26     int xh = 0;  //序号
 27     while(M--)  28  {  29         xh++;  30         bool flag = false;   //false时输出NA
 31         int lx;     //类型
 32         cin >> lx;  33         if(lx == 1)  34         //类型1要求按分数非升序输出某个指定级别的考生的成绩
 35  {  36             char zl;  37             cin >> zl;  38             cout << "Case " << xh << ": " << lx << " " << zl << endl;  39  sort(v.begin(),v.end(),Cmp);  40             for (auto it = v.begin(); it != v.end(); it++)  41  {  42                 if(it->ID[0] == zl)  43  {  44                     flag = true;  45                     cout << it->ID << " " << it->score << endl;  46  }  47  }  48  }  49         else if(lx == 2)  50         //类型2要求将某指定考场的考生人数和总分统计输出
 51  {  52             int zl;  53             cin >> zl;  54             cout << "Case " << xh << ": " << lx << " " << zl << endl;  55             int count_r = 0,count_f=0;  //人 分
 56             for (auto it = v.begin(); it != v.end(); it++)  57  {  58                 string tempstr = "";  59                 for (int i = 1; i <= 3; i++)  60  {  61                     tempstr += it->ID[i];  62  }  63                 int temp = atoi(tempstr.c_str());  64                 if(temp == zl)  65  {  66                     count_r++;  67                     count_f += it->score;  68                     flag = true;  69  }  70  }  71             if(!flag)    //查询结果为空
 72  {  73                 cout << "NA" << endl;  74  }  75             else
 76  {  77                 cout << count_r << " " << count_f << endl;  78  }  79  }  80         else if(lx == 3)  81         //类型3要求将某指定考场的考生人数和总分统计输出
 82  {  83             string zl;  84             cin >> zl;  85             cout << "Case " << xh << ": " << lx << " " << zl << endl;  86             int a[1000];  87             memset(a,0,sizeof(a));  88             for (auto it = v.begin(); it != v.end(); it++)  89  {  90                 string tempdata = "";  //日期
 91                 for (int i = 4;i <= 9; i++)  92  {  93                     tempdata += it->ID[i];  94  }  95                 if(tempdata == zl)  96  {  97                     flag = true;  98                     string tempstr = "";  //考场
 99                     for(int i=1;i<=3;i++) 100  { 101                         tempstr += it->ID[i]; 102  } 103                     int temp = atoi(tempstr.c_str()); 104                     a[temp]++; 105  } 106  } 107             if(!flag) 108  { 109                 cout << "NA" << endl; 110  } 111             else
112  { 113                 for (int i = 100; i > 0; i--) 114  { 115                     for (int j = 100; j < 1000; j++) 116  { 117                         if(a[j] == i) 118                         cout << j << " " << a[j] << endl; 119  } 120  } 121  } 122 
123  } 124  } 125     return 0; 126 }

19分代码:

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

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 struct stu   //考生
 5 {  6     string ID;   //准考证号
 7     int score;   //分数
 8 };  9 
 10 struct room   //考场
 11 {  12     string ID;   //考场编号
 13     int count;    //考场人数
 14 };  15 
 16 bool Cmp1(stu a,stu b)   //类型1用到的比较方法
 17 {  18     //lambda表达式,先按分数降序排列,若分数相等则按准考证号升序排列
 19     return a.score!=b.score? a.score>b.score : a.ID<b.ID;  20 }  21 
 22 bool Cmp3(room a,room b)   //类型3用到的比较方法
 23 {  24     //lambda表达式,先按考场人数降序排列,若考场人数相同则按考场标号升序排列
 25     return a.count!=b.count? a.count>b.count : a.ID<b.ID;  26 }  27 
 28 int main()  29 {  30     int N,M;  31     ios::sync_with_stdio(false);   //取消cin和stdin的同步
 32     cin >> N >> M;  33     vector<stu> v(N);  34     for (int i = 0; i < N; i++)  35  {  36         cin >> v[i].ID >> v[i].score;  37  }  38     int xh = 0;  //序号
 39     while(M--)  40  {  41         xh++;  42         bool flag = false;   //false时输出NA
 43         int lx;     //类型
 44         cin >> lx;  45         if(lx == 1)  46         //类型1要求按分数非升序输出某个指定级别的考生的成绩
 47  {  48             char zl;  49             cin >> zl;  50             cout << "Case " << xh << ": " << lx << " " << zl << endl;  51  sort(v.begin(),v.end(),Cmp1);  52             for (auto it = v.begin(); it != v.end(); it++)  53  {  54                 if(it->ID[0] == zl)  55  {  56                     flag = true;  57                     cout << it->ID << " " << it->score << endl;  58  }  59  }  60             if(!flag)  61  {  62                 cout << "NA" << endl;  63  }  64  }  65         else if(lx == 2)  66         //类型2要求将某指定考场的考生人数和总分统计输出
 67  {  68             int zl;  69             cin >> zl;  70             cout << "Case " << xh << ": " << lx << " " << zl << endl;  71             int count_r = 0,count_f=0;  //人 分
 72             for (auto it = v.begin(); it != v.end(); it++)  73  {  74                 string tempstr = it->ID.substr(1,3);  75                 int temp = atoi(tempstr.c_str());  76                 if(temp == zl)  77  {  78                     count_r++;  79                     count_f += it->score;  80                     flag = true;  81  }  82  }  83             if(!flag)    //查询结果为空
 84  {  85                 cout << "NA" << endl;  86  }  87             else
 88  {  89                 cout << count_r << " " << count_f << endl;  90  }  91  }  92         else if(lx == 3)  93         //类型3要求将某指定考场的考生人数和总分统计输出
 94  {  95             string zl;  96             cin >> zl;  97             cout << "Case " << xh << ": " << lx << " " << zl << endl;  98             vector<room> kc;  99             map<string,int> m; 100             for (auto it = v.begin(); it != v.end(); it++) 101  { 102                 if(it->ID.substr(4,6) == zl) 103  { 104                     flag = true; 105                     string tempstr = it->ID.substr(1,3);  //考场 106                     //int temp = atoi(tempstr.c_str());
107                     m[tempstr]++; 108  } 109  } 110             for (auto it : m)   //把map全部推入vector中来排序
111  { 112  kc.push_back({it.first,it.second}); 113  } 114  sort(kc.begin(),kc.end(),Cmp3); 115             if(!flag) 116  { 117                 cout << "NA" << endl; 118  } 119             else
120  { 121                 for (int i = 0; i < kc.size(); i++) 122  { 123                     cout << kc[i].ID  << " " << kc[i].count << endl; 124  } 125  } 126  } 127  } 128     return 0; 129 }

22分代码:

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

 1 #include <bits/stdc++.h>
 2 using namespace std;  3  
 4 struct stu   //考生
 5 {  6     string ID;   //准考证号
 7     int score;   //分数
 8 };  9  
 10 struct room   //考场
 11 {  12     string ID;   //考场编号
 13     int count;    //考场人数
 14 };  15  
 16 bool Cmp1(stu &a,stu &b)   //类型1用到的比较方法
 17 {  18     //lambda表达式,先按分数降序排列,若分数相等则按准考证号升序排列
 19     return a.score!=b.score? a.score>b.score : a.ID<b.ID;  20 }  21  
 22 bool Cmp3(room &a,room &b)   //类型3用到的比较方法
 23 {  24     //lambda表达式,先按考场人数降序排列,若考场人数相同则按考场标号升序排列
 25     return a.count!=b.count? a.count>b.count : a.ID<b.ID;  26 }  27  
 28 int main()  29 {  30     int N,M;  31     ios::sync_with_stdio(false);   //取消cin和stdin的同步
 32     cin >> N >> M;  33     vector<stu> v(N);  34     for (int i = 0; i < N; i++)  35  {  36         cin >> v[i].ID >> v[i].score;  37  }  38     int xh = 0;  //序号
 39     while(M--)  40  {  41         xh++;  42         bool flag = false;   //false时输出NA
 43         int lx;     //类型
 44         cin >> lx;  45         if(lx == 1)  46         //类型1要求按分数非升序输出某个指定级别的考生的成绩
 47  {  48             char zl;  49             cin >> zl;  50             cout << "Case " << xh << ": " << lx << " " << zl << endl;  51  sort(v.begin(),v.end(),Cmp1);  52             for (auto it = v.begin(); it != v.end(); it++)  53  {  54                 if(it->ID[0] == zl)  55  {  56                     flag = true;  57                     cout << it->ID << " " << it->score << endl;  58  }  59  }  60             if(!flag)  61  {  62                 cout << "NA" << endl;  63  }  64  }  65         else if(lx == 2)  66         //类型2要求将某指定考场的考生人数和总分统计输出
 67  {  68             int zl;  69             cin >> zl;  70             cout << "Case " << xh << ": " << lx << " " << zl << endl;  71             int count_r = 0,count_f=0;  //人 分
 72             for (auto it = v.begin(); it != v.end(); it++)  73  {  74                 string tempstr = it->ID.substr(1,3);  75                 int temp = atoi(tempstr.c_str());  76                 if(temp == zl)  77  {  78                     count_r++;  79                     count_f += it->score;  80                     flag = true;  81  }  82  }  83             if(!flag)    //查询结果为空
 84  {  85                 cout << "NA" << endl;  86  }  87             else
 88  {  89                 cout << count_r << " " << count_f << endl;  90  }  91  }  92         else if(lx == 3)  93         //类型3要求将某指定考场的考生人数和总分统计输出
 94  {  95             string zl;  96             cin >> zl;  97             cout << "Case " << xh << ": " << lx << " " << zl << endl;  98             vector<room> kc;  99             map<string,int> m; 100             for (auto it = v.begin(); it != v.end(); it++) 101  { 102                 if(it->ID.substr(4,6) == zl) 103  { 104                     flag = true; 105                     string tempstr = it->ID.substr(1,3);  //考场 106                     //int temp = atoi(tempstr.c_str());
107                     m[tempstr]++; 108  } 109  } 110             for (auto it : m)   //把map全部推入vector中来排序
111  { 112  kc.push_back({it.first,it.second}); 113  } 114  sort(kc.begin(),kc.end(),Cmp3); 115             if(!flag) 116  { 117                 cout << "NA" << endl; 118  } 119             else
120  { 121                 for (int i = 0; i < kc.size(); i++) 122  { 123                     cout << kc[i].ID  << " " << kc[i].count << endl; 124  } 125  } 126  } 127  } 128     return 0; 129 }

AC代码:

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

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 struct stu   //考生
 5 {  6     string ID;   //准考证号
 7     int score;   //分数
 8 };  9 
 10 struct room   //考场
 11 {  12     string ID;   //考场编号
 13     int count;    //考场人数
 14 };  15 
 16 bool Cmp1(stu &a,stu &b)   //类型1用到的比较方法
 17 {  18     //lambda表达式,先按分数降序排列,若分数相等则按准考证号升序排列
 19     return a.score!=b.score? a.score>b.score : a.ID<b.ID;  20 }  21 
 22 bool Cmp3(room &a,room &b)   //类型3用到的比较方法
 23 {  24     //lambda表达式,先按考场人数降序排列,若考场人数相同则按考场标号升序排列
 25     return a.count!=b.count? a.count>b.count : a.ID<b.ID;  26 }  27 
 28 int main()  29 {  30     int N,M;  31     ios::sync_with_stdio(false);   //取消cin和stdin的同步
 32     cin >> N >> M;  33     vector<stu> v(N);  34     for (int i = 0; i < N; i++)  35  {  36         cin >> v[i].ID >> v[i].score;  37  }  38     int xh = 0;  //序号
 39     while(M--)  40  {  41         xh++;  42         bool flag = false;   //false时输出NA
 43         int lx;     //类型
 44         cin >> lx;  45         if(lx == 1)  46         //类型1要求按分数非升序输出某个指定级别的考生的成绩
 47  {  48             char zl;  49             cin >> zl;  50             printf("Case %d: %d %c\n", xh, lx, zl);  51  sort(v.begin(),v.end(),Cmp1);  52             for (auto it = v.begin(); it != v.end(); it++)  53  {  54                 if(it->ID[0] == zl)  55  {  56                     flag = true;  57                     printf("%s %d\n", it->ID.c_str(), it->score);  58  }  59  }  60             if(!flag)  61  {  62                 printf("NA\n");  63  }  64  }  65         else if(lx == 2)  66         //类型2要求将某指定考场的考生人数和总分统计输出
 67  {  68             int zl;  69             cin >> zl;  70             printf("Case %d: %d %d\n", xh, lx, zl);  71             int count_r = 0,count_f=0;  //人 分
 72             for (auto it = v.begin(); it != v.end(); it++)  73  {  74                 string tempstr = it->ID.substr(1,3);  75                 int temp = atoi(tempstr.c_str());  76                 if(temp == zl)  77  {  78                     count_r++;  79                     count_f += it->score;  80                     flag = true;  81  }  82  }  83             if(!flag)    //查询结果为空
 84  {  85                 printf("NA\n");  86  }  87             else
 88  {  89                 printf("%d %d\n", count_r, count_f);  90  }  91  }  92         else if(lx == 3)  93         //类型3要求将某指定考场的考生人数和总分统计输出
 94  {  95             string zl;  96             cin >> zl;  97             printf("Case %d: %d %s\n", xh, lx, zl.c_str());  98             vector<room> kc;  99             unordered_map<string,int> m; 100             for (auto it = v.begin(); it != v.end(); it++) 101  { 102                 if(it->ID.substr(4,6) == zl) 103  { 104                     flag = true; 105                     string tempstr = it->ID.substr(1,3);  //考场 106                     //int temp = atoi(tempstr.c_str());
107                     m[tempstr]++; 108  } 109  } 110             for (auto it : m)   //把map全部推入vector中来排序
111  { 112  kc.push_back({it.first,it.second}); 113  } 114  sort(kc.begin(),kc.end(),Cmp3); 115             if(!flag) 116  { 117                 printf("NA\n"); 118  } 119             else
120  { 121                 for (int i = 0; i < kc.size(); i++) 122  { 123                     printf("%s %d\n", kc[i].ID.c_str(), kc[i].count); 124  } 125  } 126  } 127  } 128     return 0; 129 }

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM