• 算24程序设计题


    使用加、减、乘、除4种运算以及括号把给出的4 个小于10的正整数连接起来得到一个表达式,并且使得所得的这个表达式的结果等于24

    这里加、减、乘、除4种运算以及括号的运算结果与运算优先级跟平常定义一致

    第一种方法:

    number = [0] * 4
    result = [''] * 4
    def PointsGame(n):
        if n == 1:
            return abs(number[0] - 24) < 1e-6
        
        for i in range(n):
            for j in range(i+1,n):
                a, b = number[i], number[j]
                number[j] = number[n-1]
                
                charA, charB = result[i],result[j]
                result[j] = result[n-1]
                
                result[i] = '(' + charA + '+' + charB + ')'
                number[i] = a + b
                if PointsGame(n-1):
                    return True
                
                result[i] = '(' + charA + '-' + charB + ')'
                number[i] = a - b
                if PointsGame(n-1):
                    return True
                
                result[i] = '(' + charA + '*' + charB + ')'
                number[i] = a * b
                if PointsGame(n-1):
                    return True
                
                if b != 0:
                    result[i] = '(' + charA + '/' + charB + ')'
                    number[i] = a / b
                    if PointsGame(n-1):
                        return True
                    
                if a != 0:
                    result[i] = '(' + charB + '/' + charA + ')'
                    number[i] = b / a
                    if PointsGame(n-1):
                        return True
                
                number[i], number[j] = a, b
                result[i], result[j] = charA ,charB
        return False
    
    if __name__ == '__main__':
        for i in range(4):
            x = input()
            number[i] = int(x)
            result[i] = x
        if PointsGame(4):
            print('YES')
            print(result[0])
        else:
            print('NO')
    

    第二种方法:

    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])
    # print(newst)
    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]+")")
    

      

      

  • 相关阅读:
    用Visual C#创建Windows服务程序
    C# WinForm窗口最小化到系统托盘
    C# ?? 运算符是什么?
    linux中守护进程启停工具start-stop-daemon
    linux shell脚本中 mode=${1:-sart} filename=${fileuser:-"filename"}
    Unix/Linux 脚本中 “set -e” 的作用
    利用Sonar定制自定义扫描规则
    docker 镜像详解
    Docker Compose使用
    docker搭建gitlab、Redmine
  • 原文地址:https://www.cnblogs.com/cnn-ljc/p/12711771.html
Copyright © 2020-2023  润新知