问题:用1,2,3,...,9组成3个三位数abc,def,和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3。输出所有解。提示:不必太动脑筋。

// 习题2-10 样例(permutation)  
  
#include <stdio.h>  
int main(void)  
{  
    int x, y, z, a[10] = {0};  
    for(x = 100; 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;  
        int i, 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;  
}

相关文章:

  • 2022-12-23
  • 2021-09-05
  • 2021-05-30
  • 2022-12-23
  • 2021-11-03
  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-01
  • 2021-10-26
  • 2021-10-04
  • 2022-02-12
  • 2021-12-04
  • 2022-12-23
相关资源
相似解决方案