PAT B1058 选择题(C++)

PAT甲级目录 | PAT乙级目录

题目描述

B1058 选择题

解题思路

用结构体存储每题的分数、正确答案及错误次数。可以用 set 或 string 存放正确答案,判断答案是否正确可以直接用 temp == ans 判断。如正确则累加分数,如错误则累加对应错误次数,同时更新最大错误次数。最后遍历题目,将错误次数最大的题目编号输出。

易错点

  • if(P[i].cnt == max_cnt) printf(" %d", i + 1);
    • 输出的编号从 1 开始
  • scanf(" %c", &d);
    • 利用适当的输入格式吸收无效空格

也许陌生的知识点

  • ans.insert(S[i])
    • 向集合中插入一个元素
  • 需要头文件 set
  • if(a == b){ }
    • 可判断两个集合是否相等
    • 需要头文件 set

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
struct Probelm{
int score, cnt = 0;
set<char> ans;
}P[105];
int main(){
int n, m, t1, t2, max_cnt = 0;
char d;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++){
scanf("%d %d %d", &P[i].score, &t1, &t2);
for(int j = 0; j < t2; j++){
scanf(" %c", &d);
P[i].ans.insert(d);
}
}
for(int i = 0; i < n; i++){
int score = 0;
for(int j = 0; j < m; j++){
do{ scanf("%c", &d); }while(d != '('); // 定位选项
scanf("%d", &t1);
set<char> temp;
for(int k = 0; k < t1; k++){
scanf(" %c", &d);
temp.insert(d);
}
if(temp == P[j].ans) score += P[j].score;//累加分数
else P[j].cnt++; // 对错误选项进行计数
max_cnt = max(P[j].cnt, max_cnt); //更新最大错误数
}
printf("%d\n", score);
}
if(max_cnt == 0) printf("Too simple\n");
else{
printf("%d", max_cnt);
for(int i = 0; i < m; i++){
if(P[i].cnt == max_cnt) printf(" %d", i + 1);
}
}
return 0;
}