hdu 1172

水题,对输入的每一条信息,都对所有的四位数逐一判断是否符合,记录下即可。

/*
* hdu1172/linux.cpp
* Created on: 2011-8-29
* Author : ben
*/
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
#include
<algorithm>
using namespace std;

bool maybeans[10005];

void work();
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
work();
return 0;
}

bool judge(int ans, int guess, int rnum, int rindex) {
int bitnum1[10], bitnum2[10], i;
memset(bitnum1,
0, sizeof(bitnum1));
memset(bitnum2,
0, sizeof(bitnum2));
for (i = 1; i <= 1000; i *= 10) {
bitnum1[(ans
% (i * 10)) / i]++;
bitnum2[(guess
% (i * 10)) / i]++;
}
rnum
= 4 - rnum;
rnum
<<= 1;
for (i = 0; i < 10; i++) {
rnum
-= abs(bitnum1[i] - bitnum2[i]);
}
if (rnum) {
return false;
}
while (ans > 0 && guess > 0) {
if (ans % 10 == guess % 10) {
rindex
--;
}
ans
/= 10;
guess
/= 10;
}
return rindex == 0;
}

void work() {
int N, a, b, c, i, ansnum, j;
while (scanf("%d", &N) == 1 && N > 0) {
memset(maybeans,
1, sizeof(maybeans));
ansnum
= 9000;
for (i = 0; i < N; i++) {
scanf(
"%d%d%d", &a, &b, &c);
for (j = 1000; j < 10000; j++) {
if (maybeans[j]) {
maybeans[j]
= judge(j, a, b, c);
if (!maybeans[j]) {
ansnum
--;
}
}
}
}
if (ansnum != 1) {
puts(
"Not sure");
}
else {
j
= 1000;
while (!maybeans[j]) {
j
++;
}
printf(
"%d\n", j);
}
}
}

你可能感兴趣的:(HDU)