Guess number
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 852 Accepted Submission (s): 481
Problem Description guess digital games are one of gameboy's favorite games. The rules of the game are as follows: The computer randomly generates a four-digit number, and then the player guesses what the four-digit number is. Each time you guess a number, the computer will tell the player to guess a few numbers, and several of them are in the correct position.
For example, the random number generated by a computer is 1122. If a player guesses 1234, because the numbers 1 and 2 exist in both numbers, and the positions of 1 in the two numbers are the same, the computer will tell the player to guess the two numbers, one of them is in the correct position. If a player guesses 1111, the computer will tell him to guess two numbers, two in the correct position.
Now, let's give you a conversation between gameboy and the computer. Your task is to determine what the four digits are based on this conversation.
There are multiple groups of Input data. The first behavior in each group is a positive integer N (1 <=n <= 100), indicating that there are N questions and answers in this dialog. In the next N rows, each row has three integers A, B, and C. Gameboy guessed that the four-digit number is A, and then the computer answered the correct B number, where C is in the correct position. When N = 0, the input data ends.
Output: each group of input data corresponds to a row of Output. If the four-digit number can be determined based on this dialog, the four-digit number is output. If Not, the "Not sure" is output ".
Sample Input6
4815 2 1
5716 1 0
7842 1 0
4901 0 0
8585 3 3
8555 3 2
2
4815 0 0
2999 3 3
0
Sample Output3585
Not sure
Authorlwg
#include<stdio.h>
bool check1(int num1,int num2,int t)
{
int a[4],b[4];
int c[4];
int i,j;
for(i=0;i<4;i++)
{
a[i]=num1%10;
num1/=10;
b[i]=num2%10;
num2/=10;
c[i]=0;
}
int m=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(c[j]==0&&a[i]==b[j])
{
m++;
c[j]=1;
break;
}
if(m==t) return true;
else return false;
}
bool check2(int num1,int num2,int t)
{
int a[4],b[4];
int i,j;
int m=0;
for(i=0;i<4;i++)
{
a[i]=num1%10;
num1/=10;
b[i]=num2%10;
num2/=10;
if(a[i]==b[i]) m++;
}
if(m==t) return true;
else return false;
}
int main()
{
int a[101],b[101],c[101];
int cnt,res;
int n,i,j;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%d%d%d",&a[i],&b[i],&c[i]);
cnt=0;
for(i=1000;i<=9999;i++)
{
for(j=0;j<n;j++)
{
if(check1(i,a[j],b[j])==false)break;
if(check2(i,a[j],c[j])==false)break;
}
if(j>=n)
{
cnt++;
res=i;
}
if(cnt>=2)break;
}
if(cnt==0||cnt>=2) printf("Not sure\n");
else printf("%d\n",res);
}
return 0;
}