批改多选题是比较麻烦的事情,本题就请你写个程序帮助老师批改多选题,并且指出哪道题错的人最多。
输入格式:
输入在第一行给出两个正整数 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
思路:利用两个vector——结构体vector和string-vector。其中结构体vector存储每道题的总分,选项数,正确选项数和答案。string-vector用来存储每个学生输入的答案(去掉括号的版本)。然后用一个for循环一道一道的判断。
因为每个vector操作都有点的复杂,所以写一个测试一个:
先测试结构体vector
拿题目给的数据当作测试数据。我们要正确的读入数据之后,看看能否按照
正确选项数 选项
的顺序输出
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cctype>
using namespace std;
struct que
{
int total;
int num;
int true_num;
string ans;
};
int main()
{
int n, m;
cin >> n >> m;
vector<que> vec;
for (int i = 0; i < m; i++)
{
que nair;
cin >> nair.total >> nair.num >> nair.true_num;
for (int j = 0; j < nair.true_num; j++)
{
char each_ans;
cin >> each_ans;
nair.ans += each_ans;
}
vec.push_back(nair);
}
for (int i = 0; i < vec.size(); i++)
cout << vec[i].true_num << ' ' << vec[i].ans << endl;
return 0;
}
3 4
3 4 2 a c
2 5 1 b
5 3 2 b c
1 5 4 a b d e
输出:
2 ac
1 b
2 bc
4 abde
结构体vector没问题,接下来写string-vector。
依旧是用题目给的数据,我们要实现每行都输出无括号版本的同学答案
这里我使用了队列来剔除括号,参考了利用栈匹配括号的思路。
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cctype>
using namespace std;
struct que
{
int total;
int num;
int true_num;
string ans;
};
vector<string> convert(string s);
int main()
{
int n, m;
cin >> n >> m;
vector<que> vec;
for (int i = 0; i < n; i++)
{
int scores = 0;
string ans;
getline(cin, ans);
vector<string> re = convert(ans);
for (int j = 0; j < re.size(); j++)
cout << re[j] << ' ';
cout << endl;
}
system("pause");
return 0;
}
vector<string> convert(string s)
{
queue<char> q;
vector<string> vec;
int flag = 0;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '(') //如果是左括号则入队
{
q.push(s[i]);
flag = 1;
}
else if (flag == 1 && s[i] != ' ' && s[i] != ')') //队列中有左括号且当前字符不为空或者右括号则入队
q.push(s[i]);
else if (s[i] == ')')
{
q.pop(); //先把队列中的左括号出队
string str;
while (!q.empty())
{
str += q.front(); //str存储括号中的内容
q.pop();
}
flag = 0; //因为左括号出队了,所以这里置0
vec.push_back(str); //列入vector中
}
}
return vec;
}
输出:
(2 a c) (3 b d e) (2 a c) (3 a b e)
2ac 3bde 2ac 3abe
(2 a c) (1 b) (2 a b) (4 a b d e)
2ac 1b 2ab 4abde
实现成功!
接下来只要将这两个vector操作合并之后,加入判断得分操作以及排序操作即可
完整代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cctype>
using namespace std;
vector<string> convert(string s);
struct que
{
int id;
int wrong_times;
int total;
int num;
int true_num;
string s;
que()
{
wrong_times = 0;
}
};
bool cmp(que a, que b)
{
if (a.wrong_times != b.wrong_times)
return a.wrong_times > b.wrong_times;
else
return a.id < b.id;
}
int main()
{
int n, m;
cin >> n >> m;
vector<que> vec;
for (int i = 0; i < m; i++)
{
que nair;
cin >> nair.total >> nair.num >> nair.true_num;
nair.id = i + 1;
for (int j = 0; j < nair.true_num; j++)
{
char each_ans;
cin >> each_ans;
nair.s += each_ans;
}
vec.push_back(nair);
}
getchar();
for (int i = 0; i < n; i++)
{
int scores = 0;
string ans;
getline(cin, ans);
vector<string> re = convert(ans);
for (int j = 0; j < m; j++)
{
if (re[j][0] - '0' != vec[j].true_num) //如果学生某一题选项个数和正确答案个数不同,则一定错
vec[j].wrong_times++;
else if (re[j].substr(1, re[j][0] - '0') != vec[j].s) //用substr获取正确答案
vec[j].wrong_times++;
else
scores += vec[j].total;
}
cout << scores << endl;
}
sort(vec.begin(), vec.end(), cmp);
if (vec[0].wrong_times == 0)
cout << "Too simple";
else
{
cout << vec[0].wrong_times << ' ' << vec[0].id;
for (int i = 1; i < vec.size(); i++)
{
if (vec[i].wrong_times == vec[i - 1].wrong_times)
cout << ' ' <<vec[i].id;
else
break;
}
}
return 0;
}
vector<string> convert(string s)
{
queue<char> q;
vector<string> vec;
int flag = 0;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '(')
{
q.push(s[i]);
flag = 1;
}
else if (flag == 1 && s[i] != ' ' && s[i] != ')')
q.push(s[i]);
else if (s[i] == ')')
{
q.pop();
string str;
while (!q.empty())
{
str += q.front();
q.pop();
}
flag = 0;
vec.push_back(str);
}
}
return vec;
}