题解 | #24点游戏算法#
24点游戏算法
https://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb
#暴力法的修改,可以满足2 5 2 7 import itertools def appends(lst,a,b): lst.append(a+b) lst.append(a-b) lst.append(a*b) if b != 0: lst.append(a/b) return lst def solution(a,b,c,d): for nums in itertools.permutations([a,b,c,d]): a,b,c,d = nums lst16 = [] lst64 = [] lst4 = appends([],a,b) lst8 = appends([],c,d) for i in lst4: lst16 = appends(lst16,i,c) for j in lst8: lst64 = appends(lst64,i,j) for i in lst16: lst64 = appends(lst64,i,d) if 24 in lst64 or 24.0 in lst64: return True return False while True: try: a,b,c,d = list(map(int,input().split())) if solution(a,b,c,d): print('true') else: print('false') except: break