题解 | #24点游戏算法#
24点游戏算法
http://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb
学习大神思路然后再码一遍
def cal_24(s, res):
if len(s) == 1:
return s[0] == res
for i in range(len(s)):
mid = s[i]
rest = s[:i] + s[i+1:]
if cal_24(rest, res + mid) \
or cal_24(rest, res - mid) \
or cal_24(rest, res / mid) \
or cal_24(rest, res * mid):
return True
return False
while 1:
try:
print(f'{cal_24(list(map(int, input().split())), 24)}'.lower())
except:
break