PAT Basic Level 1058 选择题 解题思路及AC代码

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值