题解 | #24点游戏算法#
24点游戏算法
https://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb
ls=[] def fun1(l,s): if len(l)==0: ls.append(s) return for x in l: l1=l[:] l1.remove(x) if len(s)==0: fun1(l1,x) fun1(l1,"("+x) else: fun1(l1[:],s+"+"+x) fun1(l1[:],s+"-"+x) fun1(l1[:],s+"*"+x) fun1(l1[:],s+"/"+x) fun1(l1[:],s+"+("+x) fun1(l1[:],s+"-("+x) fun1(l1[:],s+"*("+x) fun1(l1[:],s+"/("+x) fun1(l1[:],s+"+"+x+")") fun1(l1[:],s+"-"+x+")") fun1(l1[:],s+"*"+x+")") fun1(l1[:],s+"/"+x+")") l=input().split() fun1(l,"") flag="false" for x in ls: try: if eval(x)%1==0 and int(eval(x))==24: flag="true" break except: continue print(flag)