Number Guessing | ||
| ||
description | ||
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?
| ||
input | ||
| ||
output | ||
| ||
sample_input | ||
| ||
sample_output | ||
2134 0734 |
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int num,x,y,n;
int vis[11111],dig[10],a[11111];
char str[10];
int rt(int num,int now)
{
int ret=0;
for(int i=0; i<4; i++)
{
if(num%10==now%10)
{
ret++;
}
num/=10;
now/=10;
}
return ret;
}
int pos(int num,int now)
{
int dig1[10],dig2[10],ret=0;
for(int i=0; i<4; i++)
{
dig1[i]=num%10;
num/=10;
dig2[i]=now%10;
now/=10;
}
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(dig1[i]==dig2[j])
ret++;
}
}
return ret;
}
int main()
{
int cnt=0,ans;
for(int i=0; i<=9999; i++)
{
dig[0]=i%10;
dig[1]=(i/10)%10;
dig[2]=(i/100)%10;
dig[3]=(i/1000)%10;
if(dig[0]==dig[1]||dig[0]==dig[2]||dig[0]==dig[3]||dig[1]==dig[2]||dig[1]==dig[3]||dig[2]==dig[3])
continue;
a[cnt++]=i;
}
//for(int i=0;i<cnt;i++) cout<<a[i]<<" ";
while(~scanf("%d",&n))
{
if(n<=0) break;
memset(vis,0,sizeof(vis));
for(int i=0; i<n; i++)
{
scanf("%d%s",&num,str);
x=str[0]-'0';
y=str[2]-'0';
//cout<<"x="<<x<<" y="<<y<<endl;
for(int i=0; i<cnt; i++)
{
if(vis[i]) continue;
//cout<<"ai="<<a[i]<<endl;
if(rt(num,a[i])!=x||pos(num,a[i])!=y)
vis[i]=1;
else
{
ans=a[i];
//cout<<"==="<<ans<<endl;
}
}
}
//cout<<"ans="<<ans<<endl;
dig[0]=ans%10;
dig[1]=(ans/10)%10;
dig[2]=(ans/100)%10;
dig[3]=(ans/1000)%10;
for(int i=3; i>=0; i--)
{
printf("%d",dig[i]);
}
printf("\n");
}
return 0;
}