猫史档案馆


【Python作品分享】24点查询器【作品秀】

用户:.................20.................20查看:0 回复:2 评论:0 创建时间:2021-09-07T18:58:21


【作品展示】

center_image

 

【作品介绍】

可以用两位数了。。。

 

【作品源代码】

import math


pa = ['+', '-', '*', '/']
pa_s = []
for i in range(len(pa)):
    a = pa[i]
    for j in range(len(pa)):
        b = pa[j]
        for k in range(len(pa)):
            c = pa[k]
            pa_s.append([a, b, c])

def plus(na, p, nb):
    if (p == '+'):
        return (na + nb)
    if (p == '-'):
        return (na - nb)
    if (p == '*'):
        return (na * nb)
    if (p == '/'):
        if (nb == 0):
            return 0
        else:
            return (na / nb)

def cr(plus_l, num_l, ty):

    t = ty.split('x')
    an = t[0]
    for i in range(1, 4):
        an += str(num_l[(i - 1)])
        an += t[(i * 2 - 1)]
        an += plus_l[(i - 1)]
        an += t[(i * 2)]
    an += str(num_l[-1])
    an += t[-1]
    return an

def check(plus_l, num_l):
    A = plus(num_l[0], plus_l[0], num_l[1])
    A = plus(A, plus_l[1], num_l[2])
    A = plus(A, plus_l[2], num_l[3])
    if (A == 24):
        if (plus_l[2] == '*' or plus_l[2] == '/' and plus_l[1] == '+' or plus_l[1] == '-'):
            return cr(plus_l, num_l, '(x x x x x)x x ')
        elif (plus_l[1] == '*' or plus_l[1] == '/' and plus_l[0] == '+' or plus_l[0] == '-'):
            return cr(plus_l, num_l, '(x x x)x x x x ')
        else:
            return cr(plus_l, num_l, ' x x x x x x x ')
    A = plus(num_l[1], plus_l[1], num_l[2])
    A = plus(num_l[0], plus_l[0], A)
    A = plus(A, plus_l[2], num_l[3])
    if (A == 24):
        if (plus_l[2] == '*' or plus_l[2] == '/' and plus_l[0] == '+' or plus_l[0] == '-'):
            return cr(plus_l, num_l, '[x x(x x x)]x x ')
        else:
            return cr(plus_l, num_l, ' x x(x x x)x x ')
    A = plus(num_l[1], plus_l[1], num_l[2])
    A = plus(A, plus_l[2], num_l[3])
    A = plus(num_l[0], plus_l[0], A)
    if (A == 24):
        if (plus_l[2] == '*' or plus_l[2] == '/' and plus_l[1] == '+' or plus_l[1] == '-'):
            return cr(plus_l, num_l, ' x x[(x x x)x x]')
        return cr(plus_l, num_l, 'x x(x x x x x)')
    A = plus(num_l[2], plus_l[2], num_l[3])
    A = plus(num_l[1], plus_l[1], A)
    A = plus(num_l[0], plus_l[0], A)
    if (A == 24):
        return cr(plus_l, num_l, ' x x[x x(x x x)]')
    A = plus(num_l[0], plus_l[0], num_l[1])
    B = plus(num_l[2], plus_l[2], num_l[3])
    A = plus(A, plus_l[1], B)
    if (A == 24):
        return cr(plus_l, num_l, '(x x x)x(x x x)')
    return None

def twenty_four(putin):
    global pa_s
    putin = putin.split(' ')
    w = int(putin[0])
    x = int(putin[1])
    y = int(putin[2])
    z = int(putin[3])

    nums = []
    num = [w, x, y, z]
    for i in range(len(num)):
        a = num.pop(i)
        for j in range(len(num)):
            b = num.pop(j)
            for k in range(len(num)):
                c = num.pop(k)
                for l in range(len(num)):
                    d = num.pop(l)
                    nums.append([a, b, c, d])
                num = [w, x, y, z]
                num.pop(i)
                num.pop(j)
            num = [w, x, y, z]
            num.pop(i)
        num = [w, x, y, z]
    for k in pa_s:
        for i in nums:
            A = check(k, i)
            if A:
                return A
    return False

print('输入四个数字,数字之间带空格,如:1 2 3 4')
while True:
    x = input('')
    print(twenty_four(x))

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
AlcalaAlcala

喵d

点赞0


评论


Python-happyyyyPython-happyyyy

挺好imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%82%B9%E8%B5%9E.gif"alt="emotion_编程猫_点赞"

点赞0


评论