《算法竞赛入门经典》习题2-10 排列(permutation)

排列(permutation)

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

 

#include <stdio.h>

int
main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    int f = 0;
    int g = 0;
    int h = 0; 
    int i = 0;

    int abc = 0;
    int def = 0;
    int ghi = 0;

    int x[3] = {0};
    int y[3] = {0};
    int z[3] = {0};

    int j = 0;
    int k = 0;
    
    int gate = 0;

    /* abc最小为123,ghi最大为987,故abc最小为329 */
    for (abc = 123; abc < 329; abc++)
    {
        a = abc / 100; 
        b = abc / 10 % 10; 
        c = abc % 10; 
    
        /* abc中排除0 */
        if ((0 == a) || (0 == b) || (0 == c))
        {
            continue;
        }

        /* abc中排除相同数字 */
        if ((a == b) || (a == c) || (b == c))
        {
            continue;
        }

        /* abc通过了,开始计算def */
        def = 2 * abc; 

        d = def / 100;
        e = def / 10 % 10;
        f = def % 10;

        /* def中排除0 */
        if ((0 == d) || (0 == e) || (0 == f))
        {
            continue;
        }

        /* def中排除相同数字 */
        if ((d == e) || (d == f) || (e == f))
        {
            continue;
        }

        /* def通过了,开始计算ghi */
        ghi = 3 * abc;
        
        g = ghi / 100;
        h = ghi / 10 % 10;
        i = ghi % 10;

        /* ghi中排除0 */
        if ((0 == g) || (0 == h) || (0 == i))
        {
            continue;
        }

        /* ghi中排除相同数字 */
        if ((g == h) || (g == i) || (h == i))
        {
            continue;   
        }

        x[0] = a;
        x[1] = b;
        x[2] = c;
        y[0] = d;
        y[1] = e;
        y[2] = f;
        z[0] = g;
        z[1] = h;
        z[2] = i;

        /* 比较abc和def,通过则开启gate计算ghi */
        for (j = 0; j < 3; j++)
        {
            gate = 1;
           
            if (x[j] == y[k])
            {
                /* abc与def中出现相同数字,关闭gate */
                gate = 0;
                break;
            }
    
            if (x[j] == y[k + 1])
            {
                gate = 0;
                break; 
            }
    
            if (x[j] == y[k + 2])
            {
                gate = 0;
                break;
            }
            
        }

        /* abc与def中不存在相同数字,开始计算ghi */
        if (1 == gate)
        {
            /* 比较ghi与abc、def中各数字 */
            for (j = 0; j < 3; j++)
            {
                /* 若存在相同数字,关闭gate */
                if (z[j] == x[k]) 
                {
                    gate = 0;
                    break;
                }

                if (z[j] == x[k + 1]) 
                {
                    gate = 0;
                    break;
                }

                if (z[j] == x[k + 2])
                {
                    gate = 0;
                    break;
                }
                
                if (z[j] == y[k])
                {
                    gate = 0;
                    break;   
                }

                if (z[j] == y[k + 1]) 
                {
                    gate = 0;
                    break;
                }

                if (z[j] == y[k + 2])
                {
                    gate = 0;
                    break;
                }
                
            }
            
            /* 关闭gate后,启动下一次循环 */
            if (0 == gate)
            {
                continue;
            }

            /* 输出结果 */
            printf("%d\t%d\t%d\n", abc, def, ghi);
        }
        
    }

    return 0;
}


#inclucde <stdio.h>

int
main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    int f = 0;
    int g = 0;
    int h = 0;
    int i = 0;

    int abc = 0;
    int def = 0;
    int ghi = 0;
    
    /* abc最小为123,ghi最大为987,故abc最小为329 */
    for (abc = 123; abc < 329; abc++)
    {
        def = 2 * abc;
        ghi = 3 * abc;

        a = abc / 100;
        b = abc / 10 % 10;
        c = abc % 10;

        d = def / 100;
        e = def / 10 % 10;
        f = def % 10;

        g = ghi / 100;
        h = ghi / 10 % 10;
        i = ghi % 10;

        /* 1,2,3到9相加为45,相乘为362880 */
        if (  (45     == (a + b + c + d + e + f + g + h + i))
           && (362880 == (a * b * c * d * e * f * g * h * i)))
        {
            printf("%d\t%d\t%d\n", abc, def, ghi);
        }

    }

    return 0;
}


 

 




                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值