Python calculates 2 to 9 8 numbers to satisfy ()+()=()()-()=1()
Note: each () is a number from 2-9, and they are different from each other
method one:
The sample code using the collection is
as follows:
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)
Method Two:
The most common method, using 6 loops to determine the sample code one by one is
as follows:
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)
Method three:
The sample code is as follows:
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)])