描述
Number Guessing is a computer game. First, the computer chooses four different digits, you need to guess these four digits in the fewest times,for each guess, the computer will show a judgement in
the form of "#A#B", "#" is a number 0~4. "#A" shows how many digits you guessed with both correct value and position. "#B" shows how many digits you guessed with correct value. For example, the computer chose 1234, and you guessed 6139, the computer will show
"1A2B" for you have number "1" correct value but wrong position and number "3" correct value with correct position.Thus the computer gives you the judgement of "1A2B". Now you have memorized the digits you guessed and the judgements you got, you feel like
you can figure out the correct answer. Life is filled with wisdom, isn‘t it?
输入
There are several test cases. For each test case, the first line contains a single positive integer N indicates the times you can guess,the following N lines is the record of the guess, in the form:
#### #A#B
The first four numbers is the numbers guessed,then the judgements for your guess.The input is terminated by a negative integer.
输出
For each test case, output a single line contains exactly four digits that the computer has chosen. You may assume that each test case gives you enough information, so you can figure
out the correct answer.
样例输入
21234 2A4B1243 0A4B30732 3A3B1526 0A0B4567 0A2B-1
样例输出
21340734
#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <cmath> #define N 19 using namespace std; int s[N],aa[N],bb[N],a[N]; int n; int check(int ans) { for(int k=0;k<n;k++){ int cnt=3; int ss[4]; int x=ans; for(int i=0;i<4;i++) { ss[cnt]=x%10; x/=10; cnt--; } if(ss[0]==ss[1] || ss[0]==ss[2] || ss[0]==ss[3] || ss[1]==ss[2] || ss[1]==ss[3] || ss[2]==ss[3]) return 0; int tt[4]; cnt=3; x=a[k]; for(int i=0;i<4;i++) { tt[cnt--]=x%10; x/=10; } int num=0; for(int i=0;i<4;i++) { if(tt[i]==ss[i]) { num++; } } if(num!=aa[k]) return 0; num=0; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(tt[i]==ss[j]) { num++; ss[j]=-1; } } } if(num!=bb[k]) return 0; } return 1; } int main() { while(~scanf("%d",&n)) { if(n<0) break; char x,y; for(int i=0;i<n;i++) { scanf("%d",&a[i]); scanf("%d%c%d%c",&aa[i],&x,&bb[i],&y); } int ans=0; for(int i=0;i<9999;i++) { if(check(i)) { ans=i; break; } } printf("%04d\n",ans); } return 0; }