2016第七届蓝桥杯C++B组第三题: 凑数字 (暴力法+用全排列)

题目:

凑算式

     B      DEF
A + --- + ------- = 10
     C      GHI

(如果显示有问题,可以参见【图1.jpg】)


这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。

比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。

这个算式一共有多少种解法?

注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。

这里写图片描述

思路:1:暴力法,九层循环,每一层跳过重复数字。2:用全排列函数:next_permutation,再直接判断即可。

注意:GHI 和 DEF 是一个整体,一个三位数。计算其值的时候不能直接G*H*I和D*E*F, 而是(G*100+H*10+I) 和 (D*100+E*10+F)

代码:
1.暴力
// 凑数字
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
    int cnt(0);

    ll sum1,sum2,sum3,sum4;

    for(ll a=1; a<10; ++a) {
        for(ll b=1;b<10;++b) {
            if(b==a) continue;
            for(ll c=1;c<10;++c) {
                if(c==b||c==a) continue;
                for(ll d=1;d<10;++d) {
                    if(d==c||d==b||d==a) continue;
                    for(ll e=1;e<10;++e) {
                        if(e==d||e==c||e==b||e==a) continue;
                        for(ll f=1;f<10;++f) {
                            if(f==a||f==b||f==c||f==d||f==e) continue;
                            for(ll g=1;g<10;++g) {
                                if(g==a||g==b||g==c||g==d||g==e||g==f) continue;
                                for(ll h=1;h<10;++h) {
                                    if(h==a||h==b||h==c||h==d||h==e||h==f||h==g) continue;
                                    for(ll i=1;i<10;++i) {
                                        if(i==a||i==b||i==c||i==d||i==e||i==f||i==g||i==h) continue;
                                        sum1 = a*c*(g*100+h*10+i);
                                        sum2 = b*(g*100+h*10+i);
                                        sum3 = c*(d*100+e*10+f);
                                        sum4 = 10*c*(g*100+h*10+i);
                                        if (sum1+sum2+sum3==sum4) {
                                            cout << a << ends << b << ends << c << ends;
                                            cout << d << ends << e << ends << f << ends;
                                            cout << g << ends << h << ends << i << ends;
                                            cout << endl; 
                                            cnt += 1;
                                        }

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }   

    cout << cnt << endl;

    return 0;
} 
2.用全排列函数:next_permutation
// 凑数字   全排列方法
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
    // a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
    // A    B    C    D    E    F    G    H    I

    ll sum1, sum2, sum3, sum4;  
    int a[10], cnt(0);
    for(int i=1;i<10;++i) a[i] = i;

    do {
        sum1 = a[1]*a[3]*(a[7]*100+a[8]*10+a[9]);
        sum2 = a[2]*(a[7]*100+a[8]*10+a[9]);
        sum3 = a[3]*(a[4]*100+a[5]*10+a[6]);
        sum4 = 10*a[3]*(a[7]*100+a[8]*10+a[9]);
        if(sum1+sum2+sum3==sum4) cnt += 1;
    }while(next_permutation(a+1,a+10));

    cout << cnt << endl;



    return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值