PTA 1058 multiple choice (20 points) C++ implementation

PTA 1058 multiple choice (20 points) C++ implementation

Correcting multiple-choice questions is a troublesome thing. For this question, please write a program to help the teacher correct multiple-choice questions, and point out which question has the most mistakes.

Input format:

Input Two positive integers N (≤ 1000) and M (≤ 100) are given in the first line, which are the number of students and the number of multiple-choice questions, respectively. Followed by M lines, each line sequentially gives the full score value of a question (a positive integer not exceeding 5), the number of options (a positive integer not less than 2 and not exceeding 5), the number of correct options (not exceeding the number of options positive integer of number), all correct options. Note that the options for each question are arranged sequentially starting from the lowercase English letter a. Each item is separated by 1 space. In the last N lines, each line gives a student's answer to each question. The format of the answer to each question is (the number of options selected, option 1...), given in the order of the questions. Note: The question guarantees that the students' answers to the questions are legal, that is, there is no situation where the number of selected options exceeds the actual number of options.

Output format:

Gives the scores for each student in the order they were entered, one row per score. Note that when judging a question, you can only get the score for the question if you choose all correct. The last line outputs the number of mistakes and the number of the question with the most mistakes (the questions are numbered from 1 in the order entered). If there is a tie, it will be output in ascending order of number. Numbers are separated by spaces, and there must be no extra spaces at the beginning and end of the line. If all questions are correct, output Too simple in the last line.

Input sample:

3 4
3 4 2 ac
2 5 1 b
5 3 2 bc
1 5 4 abde
(2 ac) (2 bd) (2 ac) (3 abe) (2 ac)
(1 b) (2 ab) (4 abde)
(2 bd) (1 e) (2 bc) (4 abcd)
empty line

Sample output:

3
6
5
2 2 3 4
no blank line at the end

Problem-solving ideas:

Look at the code, super detailed, you will be able to understand
the specific method:
the real answer:
2 ac
1 b
2 bc
4 abde
turn it into an array ans: 2ac 1b 2bc 4abde
student's answer:
(2 ac) (2 bd) ( 2 ac) (3 abe)
(2 ac) (1 b) (2 ab) (4 abde)
(2 bd) (1 e) (2 bc) (4 abcd)
turns this into:
2ac 2bd 2ac 3abe followed by And so on

Code example:

#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++;
			}
		}
	}
}

operation result:

insert image description here

Guess you like

Origin blog.csdn.net/m0_53209892/article/details/119305379