码迷,mamicode.com
首页 > 其他好文 > 详细

算24

时间:2020-03-28 23:52:49      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:number   rem   bsp   val   app   put   cep   too   ati   

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

 

算24

标签:number   rem   bsp   val   app   put   cep   too   ati   

原文地址:https://www.cnblogs.com/670ling/p/12589848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!