思路:假设答案是x,那么x必定满足所有语句给定的条件。例如3585和4815就有2个相同的数,1和相同的位。
只要这个数满足所有条件,那么就是一个可能的答案,当答案数量超过1个时,就是"Not sure"
AC代码
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 100 + 5;
struct node{
int num[4];
int x, y;
}a[maxn];
int w[4], vis[4];
bool is_ok(int ind) {
memset(vis, 0, sizeof(vis));
int c1 = 0, c2 = 0;
for(int i = 0; i < 4; ++i) {
if(w[i] == a[ind].num[i]) ++c2;
}
for(int i = 0; i < 4; ++i) {
for(int j = 0; j < 4; ++j) {
if(vis[j]) continue;
if(w[i] == a[ind].num[j] ) {
vis[j] = 1;
++c1;
break;
}
}
}
return c1 == a[ind].x && c2 == a[ind].y;
}
int cnt;
int main() {
int n;
while(scanf("%d", &n) == 1 && n) {
cnt = 0;
int num, ans;
for(int i = 0; i < n; ++i) {
scanf("%d%d%d", &num, &a[i].x, &a[i].y);
int cur = 3;
while(num > 0) {
a[i].num[cur--] = num % 10;
num /= 10;
}
}
for(int i = 1000; i < 10000; ++i) {
int t = i;
int cur = 3;
while(t > 0) {
w[cur--] = t % 10;
t /= 10;
}
int flag = 1;
for(int j = 0; j < n; ++j) {
if(!is_ok(j)) {
flag = 0;
break;
}
}
if(flag) {
ans = i;
++cnt;
}
if(cnt >= 2) break;
}
if(cnt >= 2) printf("Not sure\n");
else printf("%d\n", ans);
}
return 0;
}
如有不当之处欢迎指出!