题解 | #24点游戏算法#
24点游戏算法
https://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb
#递归法 来自力扣 import math def judge_point24(cards): if len(cards) == 1: return math.isclose(cards[0],24) #浮点数会导致误差 for i in range(len(cards)): a = cards.pop(0) #取第一个数 for j in range(len(cards)): b = cards.pop() #取第二个数 if b == 0: lst = [a+b,a*b,a-b] else: lst = [a+b,a-b,a*b,a/b] for value in lst: cards.append(value) #剩下三个数 if judge_point24(cards): #递归 return True cards.pop() cards.append(b) #前边的数用过放到后边 cards.append(a) return False cards = list(map(int,input().split())) if judge_point24(cards): print('true') else: print('false')