题解 | #24点游戏算法#

24点游戏算法

https://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb

def cal_4(lst, result):
    """
    :param lst: 等待计算的数字组成列表
    :param result: 计算结果
    :return: 是否能达到 ture false
    """
    if len(lst) == 1:
        return result == lst[0]
    for i in range(len(lst)):
        lst01 = lst[:i] + lst[i + 1 :]
        if (
            cal_4(lst01, result + lst[i])
            or cal_4(lst01, result - lst[i])
            or cal_4(lst01, result * lst[i])
            or cal_4(lst01, result / lst[i])
        ):
            return 1


while 1:
    try:
        lst = list(map(int, input().split()))
        r = cal_4(lst, 24)
        print("true" if r == 1 else "false")
    except:
        break

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务