比如计算机随机产生的数字为1122。如果玩家猜1234,因为1,2这两个数字同时存在于这两个数中,而且1在这两个数中的位置是相同的,所以计算机会告诉玩家猜对了2个数字,其中一个在正确的位置。如果玩家猜1111,那么计算机会告诉他猜对2个数字,有2个在正确的位置。
现在给你一段gameboy与计算机的对话过程,你的任务是根据这段对话确定这个四位数是什么。
6 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
3585 Not sure
刚开始看这道题目的时候没有什么头绪,后来看了两个题解发现还是挺简单的,因为是4位数字数据量很小,我们完全可以从1000枚举到9999.
首先检查C位置,如果一个数的某几个位置数正确,那么加入咱们猜的这个数字是正确的那么它的某几个位肯定和猜的那个数一样。
再检查B位置,一共有几个数字是正确的。
代码如下:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct Nod{
int a,b,c;
}array[111];
bool judge(int k,int num)
{
int num1[5],num2[5];
int mark[5];
memset(mark,0,sizeof(mark));
num1[0] = (array[k].a) / 1000;
num1[1] = (array[k].a % 1000) / 100;
num1[2] = (array[k].a % 100) / 10;
num1[3] = (array[k].a % 10);
num2[0] = (num) / 1000;
num2[1] = (num % 1000) / 100;
num2[2] = (num % 100) / 10;
num2[3] = (num % 10);
int i,j;
int count = 0;
for(i = 0;i < 4;i++)
{
if(num1[i] == num2[i])
count++;
}
if(count != array[k].c)
return false;
count = 0;
for(i = 0;i < 4;i++)
{
for(j = 0;j < 4;j++)
{
if(num1[i] == num2[j]&&mark[j] != 1)
{
mark[j] = true;
count++;
break;
}
}
}
if(count != array[k].b)
return false;
return true;
}
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
if(n == 0)
break;
for(i = 0;i < n;i++)
{
cin>>array[i].a>>array[i].b>>array[i].c;
}
bool flag = true;
int count = 0,result;
for(i = 1000;i<=9999;i++)
{
for(j = 0;j < n;j++)
{
flag = judge(j,i);
if(!flag)
break;
}
if(flag)
{
count++;
result = i;
}
}
if(count == 1)
cout<<result<<endl;
else
cout<<"Not sure"<<endl;
}
return 0;
}