PAT甲级刷题之路——A1148 Werewolf - Simple Version

原题如下

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

题意

每个玩家说出一个人是狼或者人,其中有两个人说谎,且其中一个为狼。

自己的想法

三层循环就可以解决。其中一二层表示狼的标号,第三层表示每个人的指向。

错误总结

错误1

每个人说的不一定是自己,也可以说说谎的人指向的不一定是自己可能是别人,但被指向的人不一定说谎。在指向的问题上栽了一个跟头。

错误2

狼的标号不一定是1位的,可能是几位,所以不能用string把两个狼的标号简单的串起来,可以用struct储存,或者在输出一次后就跳出循环,因为在循环时已经满足增序。

AC代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cctype>
#include<cstring>
//#include<stdlib>
#include<algorithm>
#include<ctime>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<set>
#include<queue>
#include<map>
#include<stack>
#include<unordered_map>
using namespace std;
const int maxn = 120;
int a[maxn], b[maxn];
struct node {
	int a, b;
	bool operator <(const node & c)const {
		if (a != c.a) {
			return a < c.a;
		}
		else
			return b < c.b;
	}
};
int main() {
#ifdef testing
	freopen("input.txt", "r", stdin);
#endif
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		//cout << a[i] << ' ';
	}
	vector<node> ans;
	//int cnt = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = i + 1; j <= n; j++) {
			vector<int> lies;
			int flag1 = 0, flag2 = 0;
			for (int p = 1; p <= n; p++) {
				if (a[p] < 0 && a[p] != -i && a[p] != -j) {
					lies.push_back(p);
					//flag1 = 1;
				}
				if (a[p] > 0 && (a[p] == i || a[p] == j)) {
					lies.push_back(p);
					//flag2 = 1;
				}
			}
			if (lies.size() == 2) {
				if (((lies[0]==i||lies[0]==j)&&lies[1]!=i&&lies[1]!=j)||((lies[1] == i || lies[1] == j) && lies[0] != i && lies[0] != j)) {
					//string str;
					//str = to_string(i) + to_string(j);
					//cout << i << ' ' << j << endl;
					//return 0;
					node c;
					c.a = i; c.b = j;
					ans.push_back(c);
				}
			}
		}
	}
	sort(ans.begin(), ans.end());
	if (ans.size() == 0) {
		cout << "No Solution" << endl;
	}
	else {
			cout << ans[0].a << ' ' << ans[0].b<< endl;
	}
	return 0;
}

结语

PAT几乎都是暴力题,能不能拿分主要在编程习惯和细心程度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值