import itertools

l=[]

for i in range(2,10):

l.append(i)

x=list(itertools.permutations(l,6))

x1=[]

x2=[]

for i in range(len(x)):

x1=x[i]

if(int(x1[0])+int(x1[1])==int(x1[2]*10)+int(x1[3])-int(x1[4])==10+int(x1[5])):

x2.append(x1)

for i in range(len(x2)):

for j in range(i):

if(x2[i][2:5]==x2[j][2:5]):

print("{}+{}={}{}-{}=1{}".format(x2[i][0],x2[i][1],x2[i][2],x2[i][3],x2[i][4],x2[i][5]))

应用规范库itertools供给的陈设因变量permutations()因变量,将2-9这八个数字掏出须要的六位举行一个全陈设拉拢

x=list(itertools.permutations(l,6))

举行一个与算式配合的估算,将那些陈设存于列表x第22中学,便于底下的去重

for i in range(len(x)):

x1=x[i]

if(int(x1[0])+int(x1[1])==int(x1[2]*10)+int(x1[3])-int(x1[4])==10+int(x1[5])):

x2.append(x1)

由于8+9后续还会展示9+8 以是要去重,由于不管是8+9仍旧9+8,反面的谜底都是23-6,反复的算式中央三位确定是十分的,切片比拟这三位,而后去重输入

for i in range(len(x2)):

for j in range(i):

if(x2[i][2:5]==x2[j][2:5]):

print("{}+{}={}{}-{}=1{}".format(x2[i][0],x2[i][1],x2[i][2],x2[i][3],x2[i][4],x2[i][5]))

截止

8+6=23-9=14

8+7=24-9=15

9+6=23-8=15

9+7=24-8=16

9+8=23-6=17