直接暴力,從1000到9999一個個試
[cpp]
#include<stdio.h>
#include<string.h>
struct op
{
int num,x,y;
}p[110];
int n;
int judge(int x,int y)
{
int a[4],b[4],a1[10],a2[10];
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
int m=p[y].num,s1=0,s2=0;
int i;
for(i=0;i<4;i++)
{
a[i]=x%10;
b[i]=m%10;
a1[a[i]]++;
a2[b[i]]++;
x/=10;
m/=10;
if(a[i]==b[i])
s2++;
}
for(i=0;i<10;i++)
{
if(a1[i]<a2[i])
s1+=a1[i];
else s1+=a2[i];
}
if(s1==p[y].x&&s2==p[y].y)
return 1;
else return 0;
}
int main()
{
int i,j,k,sum;
while(scanf("%d",&n),n)
{
sum=0;
for(i=0;i<n;i++)
scanf("%d%d%d",&p[i].num,&p[i].x,&p[i].y);
for(i=1000;i<=9999;i++)
{
for(j=0;j<n;j++)
{
if(judge(i,j)==0)
break;
}
if(j==n){sum++,k=i;}
if(sum>1)break;
}
if(sum==1)
printf("%d\n",k);
else printf("Not sure\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
struct op
{
int num,x,y;
}p[110];
int n;
int judge(int x,int y)
{
int a[4],b[4],a1[10],a2[10];
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
int m=p[y].num,s1=0,s2=0;
int i;
for(i=0;i<4;i++)
{
a[i]=x%10;
b[i]=m%10;
a1[a[i]]++;
a2[b[i]]++;
x/=10;
m/=10;
if(a[i]==b[i])
s2++;
}
for(i=0;i<10;i++)
{
if(a1[i]<a2[i])
s1+=a1[i];
else s1+=a2[i];
}
if(s1==p[y].x&&s2==p[y].y)
return 1;
else return 0;
}
int main()
{
int i,j,k,sum;
while(scanf("%d",&n),n)
{
sum=0;
for(i=0;i<n;i++)
scanf("%d%d%d",&p[i].num,&p[i].x,&p[i].y);
for(i=1000;i<=9999;i++)
{
for(j=0;j<n;j++)
{
if(judge(i,j)==0)
break;
}
if(j==n){sum++,k=i;}
if(sum>1)break;
}
if(sum==1)
printf("%d\n",k);
else printf("Not sure\n");
}
return 0;
}