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))
# 輸出結果如下
一共有18種滿足要求的算式,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/253099.html
標籤:python
上一篇:一小時教會你單執行緒爬取微博熱搜
