题解 | #24点游戏算法#

24点游戏算法

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

def f(nums,target):
    if (len(nums) == 1):
       return nums[0] == target
    for i in range(len(nums)):
        c = nums[i]  # 当前数字
        tmp = nums[:i] + nums[i+1:]  # 列表中的其他数字
        if f(tmp,target+c) or f(tmp,target-c) or f(tmp,target*c) or f(tmp,target/c):
            return True
    return False


while True:
    try:
        nums = list(map(int,input().split()))
        if f(nums,24):
            print('true')
        else:
            print('false')

    except:
        break

全部评论

相关推荐

点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
程序员饺子:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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