Python calcula los 8 números del 2 al 9 para satisfacer () + () = () () - () = 1 () Nota: Cada () es un número del 2 al 9 y son diferentes entre sí

Python calcula de 2 a 9 8 números para satisfacer () + () = () () - () = 1 ()

Nota: cada () es un número del 2 al 9 y son diferentes entre sí

método uno:

El código de muestra que usa la colección es el
siguiente:

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)

Método dos:

El método más común, que utiliza 6 bucles para determinar el código de muestra uno por uno, es el
siguiente:

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)

Método tres:

El código de muestra es el siguiente:

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)])

Supongo que te gusta

Origin blog.csdn.net/qq_44250569/article/details/109137926
Recomendado
Clasificación