首页 > 其他 > 详细

算24

时间:2020-03-28 22:56:55      阅读:76      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!