问题描述
由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。
比如: 210 x 6 = 1260 8 x 473 = 3784 27 x 81 = 2187
都符合要求。
如果满足乘法交换律的算式算作同一种情况,那么,包含上边已列出的3种情况,一共有多少种满足要求的算式。
请填写该数字,通过浏览器提交答案,不要填写多余内容(例如:列出所有算式)。
思路分析及代码实现
这道题就直接暴力解决,枚举所有四位数,先排除掉有重复数字的,然后再按照条件选出符合条件的即可
一共是12个
201 * 6 = 1206
21 * 60 = 1260
210 * 6 = 1260
15 * 93 = 1395
41 * 35 = 1435
501 * 3 = 1503
51 * 30 = 1530
510 * 3 = 1530
87 * 21 = 1827
27 * 81 = 2187
351 * 9 = 3159
473 * 8 = 3784
num = 0
p = 0
for i in range(1000, 10000):
a = str(i)
if len(set(a)) == 4:
for x in a:
for y in a:
for m in a:
for n in a:
if x != y and x != m and x != n and y != m and y != n and m != n:
if int(str(x) + str(y)) * int(str(m) + str(n)) == i and int(str(x) + str(y)) != p:
p = int(str(m) + str(n))
num += 1
elif int(str(x) + str(y) + str(m)) * int(str(n)) == i:
num += 1
print(num)