用1,2,3,...,9组成3个三位数abc, def和ghi,每个数字恰好使用一次,要求abc:def:ghi = 1:2:3。按照"abc def ghi"的格式输出所有解,每行一个解。提示:不必太动脑筋。
//2-6 version 1
#include <stdio.h>
#include <string.h>
int main()
{
int x,y,z,a[10],i,s;
memset(a,0,sizeof(a));
for(x=123;x<333;x++){
y=2*x;
z=3*x;
//令a[出现的数字]=1
a[x/100]=a[x/10%10]=a[x%10]=1;
a[y/100]=a[y/10%10]=a[y%10]=1;
a[z/100]=a[z/10%10]=a[z%10]=1;
s=0;
for(i=1;i<10;i++)
s+=a[i];
if(s==9)
printf("%d\t%d\t%d\n",x,y,z);
for(i=1;i<10;i++)//重新赋值为0
a[i]=0;
}
return 0;
}
//2-6 version 2
#include <stdio.h>
int main()
{
int i,i1,i2,i3,j,j1,j2,j3,k,k1,k2,k3;
int c, count;
for (i=123;i<333;i++) {
j=i*2;
k=i*3;
i1=i/100,i2=i/10%10,i3=i%10;
j1=j/100,j2=j/10%10,j3=j%10;
k1=k/100,k2=k/10%10,k3=k%10;
for (c=1;c<10;c++) {
count=0;
if (c==i1)
count++;
if (c==i2)
count++;
if (c==i3)
count++;
if (c==j1)
count++;
if (c==j2)
count++;
if (c==j3)
count++;
if (c==k1)
count++;
if (c==k2)
count++;
if (c==k3)
count++;
if (count>1||count==0)//同一个数字出现多次或者出现0
break;
}
if (c==10)
printf("%d %d %d\n",i,j,k);
}
return 0;
}