1.基本思路.(核心思想是排序)
2.代码
num = [1, 2, 3,4,5,6,7,8,9]
def permute(nums):
from itertools import permutations
result = []
for i in permutations(nums, len(nums)):
result.append(list(i))
for b in range(0,len(result)):
x = result[b][0]*100+result[b][1]*10+result[b][2]
y = result[b][3]*100+result[b][4]*10+result[b][5]
z = result[b][6] * 100 + result[b][7] * 10 + result[b][8]
if x + y ==z:
print("{}+{}={}".format(x,y,z))
print('\n')
print(permute(num))
1.基本思路.(核心思想是排序)
思路等等同第一种方法差不多
2.代码
num_list = [1, 2, 3,4,5,6,7,8,9]
def permute(nums):
from functools import reduce
result = [list(i) for i in
reduce(lambda x, y: [str(a) + str(b) for a in x for b in y if str(b) not in str(a)], [nums] * len(nums))]
for b in range(0,len(result)):
x = int(result[b][0])*100+int(result[b][1])*10+int(result[b][2])
y = int(result[b][3])*100+int(result[b][4])*10+int(result[b][5])
z = int(result[b][6])*100+int(result[b][7])*10+int(result[b][8])
if x + y ==z:
print("{}+{}={}".format(x,y,z))
print(permute(num_list))
图片以及下述代码引用自:https://blog.csdn.net/yangfanswt/article/details/82016430
from functools import reduce
a=reduce(lambda x,y:x+y**2,[1,2,3],0)
print(a)
关于reduce和lambda可以参考https://blog.csdn.net/yangfanswt/article/details/82016430
list = [1, 2, 3,4,5,6,7,8,9]
for a in range(1,10):
for b in range(1,10):
for c in range(1,10):
for d in range(1, 10):
for e in range(1, 10):
for f in range(1, 10):
for g in range(1,10):
for h in range(1,10):
for i in range(1,10):
if a!=b and a!=c and a!= d and a!=e and a!=f and a!=g and a!=h and a!=i:
if b!=c and b!= d and b!=e and b!=f and b!=g and b!=h and b!=i:
if c!= d and c!=e and c!=f and c!=g and c!=h and c!=i:
if d!=e and d!=f and d!=g and d!=h and d!=i:
if e!=f and e!=g and e!=h and e!=i:
if f!=g and f!=h and f!=i:
if g!=h and g!=i:
if h!=i:
if (a*100+b*10+c) + (d*100+e*10+f) == (g*100+h*10+i):
x = a*100+b*10+c
y = d*100+e*10+f
z = g*100+h*10+i
print("{}+{}={}".format(x,y,z))