用1,2....9组成三个三位数abc,def,ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3,输出所有解

来源:互联网 发布:linux常用命令mv 编辑:程序博客网 时间:2024/04/24 06:45
#include <stdio.h>
int func(int a)
{
int temp, flag[10] ={0,0,0,0,0,0,0,0,0,0};//把整个数组初始化为0;
for(temp=a%10;a;a/=10,temp=a%10)
if(flag[temp]++ == 1) return 0;//如果有相同的位,则数组的那个位会被++两次
return 1;
}
void main()
{
int base;
for(base=123; base<345; base++)
if(func(base*1000000+base*2*1000+base*3)==1)
printf("%d %d %d\n", base, base*2, base*3);
}
原创粉丝点击