python计算 2到9 8个数里面使之满足()+()=()()-()=1() 注:每个()里面是2-9当中的一个数,而且互不相同

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)])
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值