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种满足要求的算式。