PAT 乙级 1058 选择题 v1.0
1. 题目简述及在线测试位置
1.1 输入是 题目的正确答案 和 学生的答题情况,要求统计出每个学生的得分和出错次数最多的题目
1.2 在线测试位置: 1058 选择题
2. 基本思路
2.1 使用结构体数组存储题目相关信息;通过char数组存储学生的答题情况
struct Issue
{
int Score; //题目分数
int Index; //题目编号
//int Option; //对于题目选项个数,统计中用不上,因此不存储
int RightCount; //题目正确的选项数
char Right[RightMAX]; //题目正确的选项
int ErrorCount = 0; //题目被答错的次数
};
2.2 信息存储完毕,通过字符串比较函数strcmp( )进行比对,对于答案正确的学生,增加其得分
2.3 对于出错次数最多的题目,通过sort( )函数 依据 ErrorCount字段 进行非递增排序,随后打印输出即可
3. 完整AC代码
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define RightMAX 6
#define IssueMAX 101
struct Issue
{
int Score;
int Index;
int RightCount;
char Right[RightMAX];
int ErrorCount = 0;
};
bool compare(struct Issue a, struct Issue b)
{
return a.ErrorCount > b.ErrorCount;
}
int main()
{
struct Issue Problem[IssueMAX];
int Students, ProblemNum;
int Score, Option, Count;
char Symbol;
cin >> Students >> ProblemNum;
for(int i=1;i<= ProblemNum;i++)
{
cin >> Score >> Option >> Count;
Problem[i].Index = i;
Problem[i].Score = Score;
Problem[i].RightCount = Count;
int j = 0;
while (Count--)
{
cin >> Symbol;
Problem[i].Right[j++] = Symbol;
}
Problem[i].Right[j] = '\0';
}
int TotalGrade = 0;
while (Students--)
{
for (int i = 1; i <= ProblemNum; i++)
{
int SelectNum, j = 0; //学生选定的选项数 临时数据
char Select[RightMAX],tmp; //学生选定的答案 临时数据
cin >> tmp >> SelectNum; //通过tmp跳过(
for(int i=0;i< SelectNum;i++)
{
cin>>tmp;
Select[j++] = tmp;
}
Select[j] = '\0';
cin >> tmp; //通过tmp跳过 )
if (SelectNum == Problem[i].RightCount) //选定的选项数与正确选项数相等
{
if(!strcmp(Select, Problem[i].Right)) //答案正确。相等时,strcpm为0
TotalGrade += Problem[i].Score;
else
Problem[i].ErrorCount++;
}
else
Problem[i].ErrorCount++;
}
cout << TotalGrade << endl;
TotalGrade = 0;
}
sort(Problem + 1, Problem + ProblemNum, compare);
if (Problem[1].ErrorCount == 0)
cout << "Too simple";
else
{
cout << Problem[1].ErrorCount<<" "<<Problem[1].Index;
for (int i = 2; i <= ProblemNum; i++)
if(Problem[i].ErrorCount == Problem[1].ErrorCount)
cout<< " " << Problem[i].Index;
}
return 0;
}