题解 | #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

全部评论

相关推荐

钱嘛数字而已:拖拉机被发明出来之后,就不需要农民了吗?农民还是需要的,但不需要这么多了,另外对农民的要求也变高了,需要会开拖拉机。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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