算24

from itertools import permutations
n1 = input("")
n2 = input("")
n3 = input("")
n4 = input("")
n = n1+n2+n3+n4
sum = 1
for i in n:
    sum *= eval(i)
if sum < 24:
    print("no")
    exit()
notation = ['+', '-', '*', "/"]
st = set()
num = 0
number = set(permutations(n))
for i in notation:
    s = i
    t1 = notation.copy()
    t1.remove(i)
    for j in t1:
        s += j
        t2 = t1.copy()
        t2.remove(j)
        for p in t2:
            s += p
            st.add(s)
            s = i+j
        s = i
newst = set()
for i in number:
    for j in st:
        newst.add(i[0]+j[0]+i[1]+j[1]+i[2]+j[2]+i[3])
all = set()
for i in newst:
    i1 = '('+i[0:3]+')'+i[3:]
    i2 = i[0:2]+'('+i[2:5]+')'+i[5:]
    i3 = i[0:4] + '(' + i[4:] + ')'
    i4 = '(('+i[0:3]+')'+i[3:5]+")"+i[5:]
    i5 = i[0:2]+'(('+i[2:5]+')'+i[5:]+")"
    i6 = '(' + i[0:2] + '(' + i[2:5] + '))' + i[5:]
    i7 = i[0:2]+'('+i[2:4]+'('+i[4:]+"))"
    all.add(i1)
    all.add(i2)
    all.add(i3)
    all.add(i4)
    all.add(i5)
    all.add(i6)
    all.add(i7)
result = []
for i in all:
    try:
        if eval(i) == 24:
          result.append(i)
    except:
        pass
print("yes")
print("("+sorted(result)[0]+")")

 

posted @ 2020-03-28 22:20  liuchun玲  阅读(134)  评论(0编辑  收藏  举报