PTA 1058 选择题 (20 分) C++实现
批改多选题是比较麻烦的事情,本题就请你写个程序帮助老师批改多选题,并且指出哪道题错的人最多。
输入格式:
输入在第一行给出两个正整数 N(≤ 1000)和 M(≤ 100),分别是学生人数和多选题的个数。随后 M 行,每行顺次给出一道题的满分值(不超过 5 的正整数)、选项个数(不少于 2 且不超过 5 的正整数)、正确选项个数(不超过选项个数的正整数)、所有正确选项。注意每题的选项从小写英文字母 a 开始顺次排列。各项间以 1 个空格分隔。最后 N 行,每行给出一个学生的答题情况,其每题答案格式为 (选中的选项个数 选项1 ……),按题目顺序给出。注意:题目保证学生的答题情况是合法的,即不存在选中的选项数超过实际选项数的情况。
输出格式:
按照输入的顺序给出每个学生的得分,每个分数占一行。注意判题时只有选择全部正确才能得到该题的分数。最后一行输出错得最多的题目的错误次数和编号(题目按照输入的顺序从 1 开始编号)。如果有并列,则按编号递增顺序输出。数字间用空格分隔,行首尾不得有多余空格。如果所有题目都没有人错,则在最后一行输出 Too simple。
输入样例:
3 4
3 4 2 a c
2 5 1 b
5 3 2 b c
1 5 4 a b d e
(2 a c) (2 b d) (2 a c) (3 a b e)
(2 a c) (1 b) (2 a b) (4 a b d e)
(2 b d) (1 e) (2 b c) (4 a b c d)
结尾无空行
输出样例:
3
6
5
2 2 3 4
结尾无空行
解题思路:
看代码,超详细,你必然能看懂
具体做法:
真正的答案:
2 a c
1 b
2 b c
4 a b d e
将其变成数组ans:2ac 1b 2bc 4abde
学生的答案:
(2 a c) (2 b d) (2 a c) (3 a b e)
(2 a c) (1 b) (2 a b) (4 a b d e)
(2 b d) (1 e) (2 b c) (4 a b c d)
将其变成:
2ac 2bd 2ac 3abe 后面的依次类推
代码示例:
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <numeric>
#include <deque>
#include <map>
using namespace std;
int main()
{
int students, total; //学生 题目总数
cin >> students;
getchar();
cin >> total;
getchar();
vector<string> ans; //存真正的答案
vector<int> num; //存题目分数
for (int i = 0; i < total; i++)
{
int gride, sum;
cin >> gride;
getchar();
num.push_back(gride);
cin >> sum;
getchar();
string ac;
getline(cin, ac);
string temp; //去空格后给temp
for (int i = 0; i < ac.length(); i++)
{
if (ac[i] != ' ')
{
temp.push_back(ac[i]);
}
}
ans.push_back(temp);
}
/*此时:
ans:2ac 1b 2bc 4abde
num:3 2 5 1
两者一一对应,同样的下标可以得到相应的答案与分数
*/
vector<int> wrong(200); //记录每题错了几次
while (students--)
{
string key;
getline(cin, key);
string temp = ""; //去空格后给temp
for (int i = 0; i < key.length(); i++)
{
if (key[i] != ' ')
{
temp += key[i];
}
}
string solution = ""; //得到括号内的每一题的答案
vector<string> stu_ans; //存学生答案solution
int res = 0;
for (int i = 0; i < temp.length(); i++)
{
if (temp[i] == '(')
{
continue;
}
else if (temp[i] == ')')
{
stu_ans.push_back(solution);
solution = "";
}
else
{
solution += temp[i];
}
}
//真正答案与学生答案依次对比
for (int i = 0; i < stu_ans.size(); i++)
{
if (stu_ans[i] == ans[i])
{
res += num[i];
}
else
{
wrong[i]++;
}
}
cout << res << endl;
}
int max_value = *max_element(wrong.begin(), wrong.end()); //得到最大错题次数
if (max_value == 0)
{
cout << "Too simple" << endl;
}
else
{
cout << max_value << " ";
int flag = 1; //控制输出
for (int i = 0; i < wrong.size(); i++)
{
if (wrong[i] == max_value)
{
if (flag > 1)
{
cout << " ";
}
cout << i + 1;
flag++;
}
}
}
}