(编程求助)将1到9分为三个三位数,当地一个数是第二个数的二分之一且是第一个数的三分之一时输出这三个

来源:百度知道 编辑:UC知道 时间:2024/05/24 01:23:03

我想楼主是说当第一个数是第二个数的二分之一,并且是第三个数的三分之一的时候输出这3个数吧?
先将1~9分到数组里,穷举所有情况,再将满足条件的输出。
该程序主要内容我是参考别人的,只稍作修改而已。
能输出4组数据如下:
192 384 576
219 438 657
273 546 819
327 654 981

#include <stdio.h>
#include <math.h>
/*gen_perm产生字典序排列P(n,m)*/
/*产生元素用1..n表示 */
/*dummy为产生后调用的函数,传入a[]和n,a[0]..a[n-1]为一次产生的结果 */

#define MAXN 100
int count;

void dummy(int* a,int n)
{
int x,y,z;
x=a[0]*100+a[1]*10+a[2];
y=a[3]*100+a[4]*10+a[5];
z=a[6]*100+a[7]*10+a[8];
if(2*x==y && 3*x==z)
printf("%d%d%d %d%d%d %d%d%d\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
}

void _gen_perm(int* a,int n,int m,int l,int* temp,int* tag)
{
int i;
if (l==m)
dummy(temp,m);
else
for (i=0;i<n;i++)
if (!tag[i]){
temp[l]=a[i],tag[i]=1;
_gen_perm(a,n,m,l+1,temp,tag);
tag[i]=0;
}
}