LQBv18-Python:神奇算式

2014-/Province_C_C++_A/3/神奇算式

由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。

比如:

210 x 6 = 1260
8 x 473 = 3784
27 x 81 = 2187

都符合要求。

如果满足乘法交换律的算式算作同一种情况,那么,包含上边已列出的3种情况,一共有多少种满足要求的算式。

请填写该数字,通过浏览器提交答案,不要填写多余内容(例如:列出所有算式)。

解题思路:
乘法算式有两种情况,一种为一位数乘三位数,另一种为两位数乘两位数,分别对这两种情况进行讨论。

代码:

count1 = 0
for a in range(1, 10):
    for b in range(1, 10):
        if b == a:
            continue
        for c in range(0, 10):
            if c == a or c == b:
                continue
            for d in range(0, 10):
                if d == a or d == b or d == c:
                    continue
                set1 = set(str(a) + str(b) + str(c) + str(d))
                set2 = set(str(a * int(str(b) + str(c) + str(d))))
                if set1 == set2:
                    count1 += 1


count2 = 0
for a in range(1, 10):
    for b in range(0, 10):
        if b == a:
            continue
        for c in range(1, 10):
            if c == a or c == b:
                continue
            for d in range(0, 10):
                if d == a or d == b or d == c:
                    continue
                set3 = set(str(a) + str(b) + str(c) + str(d))
                set4 = set(str(int(str(a) + str(b)) * int(str(c) + str(d))))
                if set3 == set4:
                    count2 += 1


print('一共有{}种满足要求的算式。'.format(count1 + count2//2))

# 输出结果如下
一共有12种满足要求的算式。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vicky__3021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值