• 算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]+")")
  • 相关阅读:
    hdu 1978
    hdu 2700
    hdu 1176
    hdu 2390
    hdu 2707
    hdu 1804
    hdu 2703
    hdu 2572
    hdu 1171
    React有状态组件和无状态组件
  • 原文地址:https://www.cnblogs.com/670ling/p/12589848.html
Copyright © 2020-2023  润新知