原题如下
题意
每个玩家说出一个人是狼或者人,其中有两个人说谎,且其中一个为狼。
自己的想法
三层循环就可以解决。其中一二层表示狼的标号,第三层表示每个人的指向。
错误总结
错误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几乎都是暴力题,能不能拿分主要在编程习惯和细心程度。