solution
有两个狼人,其中一个狼人说谎,找到符合该条件的方案。若有多种则输出序号最小的方案,若无方案则输出No Solution
。
枚举所以狼人的可能性,找到符合的方案输出并结束。
#include<iostream>
using namespace std;
int main(){int n, lie, have, i, j, l1, l2, ans[110];scanf("%d", &n);for(i = 1; i <= n; i++){scanf("%d", ans + i);}for(i = 1; i < n; i++){for(j = i + 1; j <= n; j++){lie = have = 0;for(int k = 1; k <= n; k++){if(ans[k] < 0 && (abs(ans[k]) == i || abs(ans[k]) == j)) continue;if(ans[k] > 0 && ans[k] != i && ans[k] != j) continue;lie++;if(lie == 1) l1 = k;else l2 = k;}if(l1 == i || l1 == j) have++;if(l2 == i || l2 == j) have++;if(lie == 2 && have == 1){printf("%d %d", i, j);return 0;}}}printf("No Solution");return 0;
}
or
#include<iostream>
using namespace std;
int main(){int n, i, j, ans[110];scanf("%d", &n);for(i = 1; i <= n; i++){scanf("%d", ans + i);}for(i = 1; i < n; i++){for(j = i + 1; j <= n; j++){int lie[110] = {0}, cnt = 0, have = 0;for(int k = 1; k <= n; k++){if((ans[k] < 0 && abs(ans[k]) != i && abs(ans[k]) != j) || (ans[k] > 0 && (ans[k] == i || ans[k] == j))) lie[cnt++] = k;}if(lie[0] == i || lie[0] == j) have++;if(lie[1] == i || lie[1] == j) have++;if(cnt == 2 && have == 1){printf("%d %d", i, j);return 0;}}}printf("No Solution");return 0;
}