PAT (Basic Level) Practice (中文)1058 选择题

这个题我是先看了一遍题打了一半代码然后有事去了

今天来把它打完竟然发现和我之前看到的题目不一样,,灵异事件了

因为我印象中是有答案对但不完全是半勾,所以我还加了对给出答案数量计数的变量

懒得改了倒也也不影响后续

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
// 给问题编号
static int idcount = 1;

struct Problem  // 问题
{
	double score,ansCount;
	bool ans[26];
	int wrong; int id;
	Problem(double sc, double ac, int* an) :score(sc), ansCount(ac) { 
		for (int i = 0; i < 26; i++)ans[i] = an[i]; id = idcount++;
		wrong = 0;
	}
	bool operator<(Problem& p) {
		if (p.wrong != wrong)return p.wrong < wrong;
		else return id < p.id;
	}
};

struct Answer  // 答案
{
	Problem* toSolve; // 答案要解决的问题
	int giveCount; bool myAns[26];
	Answer(Problem* p, string a) :toSolve(p) {
		memset(myAns, 0, sizeof(myAns));
		int i; string giveC; for (i = 1; i < a.size() && isdigit(a[i]); i++) giveC += a[i];
		giveCount = atoi(giveC.c_str());
		for (; i < a.size(); i++) if (isalpha(a[i])) myAns[a[i] - 'a']=1;
	}

	bool isSame() { // 判断答案是否一致
		if (giveCount != toSolve->ansCount) { toSolve->wrong++; return 0; }
		for (int i = 0; i < 26; i++) if (toSolve->ans[i] != myAns[i]) { toSolve->wrong++; return 0; }
	return 1; }

	double getScore() {
		bool same = isSame(); if (!same) return 0;
		else return toSolve->score;
	}
};

vector<Problem> pro; // 记录问题

class Student
{
	double scoreSum;
public:
	vector<Answer> ans;
	Student(int m) {
		scoreSum = 0;
// 根据输入格式处理数据
		for (int i = 0; i < m; i++) { string str; getline(cin, str, ')'); str.erase(0, 1); ans.push_back({ &pro[i],str }); }
		for (int i = 0; i < ans.size(); i++) scoreSum += ans[i].getScore();
	}
	void showscore() { cout << scoreSum << endl; }
};
int main()
{
	int m, n;
	cin >> n >> m;
	vector<Student>stu;
	for (int i = 0; i < m; i++)
	{
		double sc, nc, ac; 
		cin >> sc >> nc >> ac; int* ans = new int[26]();
		for (int j = 0; j < ac; j++) { char ch; cin >> ch; ans[ch - 'a'] = 1; }
		pro.push_back({ sc,ac,ans });
		delete[]ans;
	}
	for (int i = 0; i < n; i++) { stu.push_back({ m }); stu[i].showscore(); }
	sort(pro.begin(), pro.end());
	if (pro[0].wrong == 0) cout << "Too simple";
	else {
		int wrong = pro[0].wrong; cout << wrong;
		for (auto &i : pro) if (i.wrong != wrong) break;
		else cout << ' ' << i.id;
	}
	return 0;
}

就用类来封装,纯粹是初学C++用来练习了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值