python计算 2到9 8个数里面使之满足()+()=()()-()=1()
注:每个()里面是2-9当中的一个数,而且互不相同
方法一:
运用集合
示例代码如下:
for a in range(2, 10):
for b in range(2, 10):
for c in range(2, 10):
for d in range(2, 10):
for e in range(2, 10):
for f in range(2, 10):
# 初始化集合,并起到重置作用
b_set = set()
b_set.update({a, b, c, d, e, f})
# 判断是否有重复元素
if (len(b_set) == 6):
if (a + b == c * 10 + d - e == 10 + f):
print(a, b, c, d, e, f)
方法二:
最普通方法,用6个循环逐一判断
示例代码如下:
for x in range(2,10):
for y in range(2,10):
if x!=y and x<y:
for j in range(2,10):
if x!=j and y!=j:
for t in range(2,10):
if x!=t and y!=t and j!=t:
for i in range(2,10):
if x!=i and y!=i and j!=i and t!=i:
for z in range(2,10):
if x!=z and y!=z and j!=z and t!=z and i!=z and x+y==10+j and x+y==i*10+t-z:
print(x,"+",y,"==",i*10+t,"-",z,"==1",j)
方法三:
示例代码如下:
from itertools import permutations
def array2(li):
# permutation方法返回一个列表,排列方式保存在元组中,每一个元组作为列表中的一个元素
num_list = list(permutations(li, 6))
# 遍历获取列表中每个元组
for num in num_list:
if (num[0] + num[1] == num[2] * 10 + num[3] - num[4] == 10 + num[5]):
print(num[0], num[1], num[2], num[3], num[4], num[5])
array2([n for n in range(2,10)])