题解 | #24点游戏算法#
24点游戏算法
https://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb
import sys
def check(numlist, res):
if len(numlist)==0:
if res == 24:
return True
else:
return False
else:
allflag = False
for i in range(len(numlist)):
left = numlist[:i]+numlist[i+1:]
v = numlist[i]
flag = check(left,res+v) or check(left,res-v) or check(left,res*v)
if v!=0:
flag = flag or check(left,res/v)
allflag = allflag or flag
return allflag
a = list(map(int,input().strip().split()))
if check(a,0):
print('true')
else:
print('false')
阿里云工作强度 694人发布