怎么用1-9个随机组成三个三位数
题目:用1、2、3…9组成三个三位数:abc、def、ghi,每个数字恰好被使用了一次,要求abc:def:ghi=1:2:3.输出所有解。想问一下各路大神,用这九个数字随机组成3个3位数的算法,有没有简单一点的?我写的每次都是有很多循环和嵌套,运行效率非常低。
#include <stdio.h> int main( void ) { for( unsigned abc=123; abc<=329; ++abc ) { unsigned def = abc*2; unsigned ghi = abc*3; unsigned mark = 0; mark |= 1u<<(abc/1%10); mark |= 1u<<(abc/10%10); mark |= 1u<<(abc/100%10); mark |= 1u<<(def/1%10); mark |= 1u<<(def/10%10); mark |= 1u<<(def/100%10); mark |= 1u<<(ghi/1%10); mark |= 1u<<(ghi/10%10); mark |= 1u<<(ghi/100%10); if( mark == 0x3FE ) // binary: 1111111110 printf( "%u:%u:%u\n", abc, def, ghi ); } return 0; }