偶然看到的题目
□□□×□□=□□□□□
在□中只能填0--9这十个数字,且每个数字只能用一次。
请高手给出代码。谢谢
import itertools
#xxx*xx=xxxxx
for i in itertools.permutations([0,1,2,3,4,5,6,7,8,9],10):
a=i[0]*100+i[1]*10+i[2]
b=i[3]*10+i[4]
c=i[5]*10000+i[6]*1000+i[7]*100+i[8]*10+i[9]
#print(i,a,b,c)
if a*b==c:
print(i)
或者
import itertools
for i in itertools.permutations([str(i) for i in range(0,10)],10):
a=i[0]+i[1]+i[2]
b=i[3]+i[4]
c=i[5]+i[6]+i[7]+i[8]+i[9]
if int(a)*int(b)==int(c):
print(i)
结果是:
(1, 3, 8, 4, 2, 0, 5, 7, 9, 6) (1, 5, 7, 2, 8, 0, 4, 3, 9, 6) (1, 5, 9, 4, 8, 0, 7, 6, 3, 2) (1, 8, 6, 3, 9, 0, 7, 2, 5, 4) (1, 9, 8, 2, 7, 0, 5, 3, 4, 6) (2, 9, 7, 1, 8, 0, 5, 3, 4, 6) (2, 9, 7, 5, 4, 1, 6, 0, 3, 8) (3, 4, 5, 7, 8, 2, 6, 9, 1, 0) (3, 6, 7, 5, 2, 1, 9, 0, 8, 4) (3, 9, 6, 4, 5, 1, 7, 8, 2, 0) (4, 0, 2, 3, 9, 1, 5, 6, 7, 8) (4, 8, 3, 1, 2, 0, 5, 7, 9, 6) (4, 9, 5, 3, 6, 1, 7, 8, 2, 0) (5, 9, 4, 2, 7, 1, 6, 0, 3, 8) (7, 1, 5, 4, 6, 3, 2, 8, 9, 0) (9, 2, 7, 6, 3, 5, 8, 4, 0, 1)