题解 | #24点运算#
24点运算
https://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
import re l = [] def fun(ls, s): if len(ls) == 1: l.append(s + "+" + ls[0]) l.append(s + "-" + ls[0]) l.append(s + "*" + ls[0]) l.append(s + "//" + ls[0]) for x in ls: l1 = ls[:] l1.remove(x) if s == "": fun(l1, s + x) else: fun(l1, s + "+" + x) fun(l1, s + "-" + x) fun(l1, s + "*" + x) fun(l1, s + "//" + x) def eval1(s): dig=re.compile("\\d+") sym=re.compile("\\D+") digs=re.findall(dig,s) syms=re.findall(sym,s) t=int(digs[0]) digs=digs[1:] for i in range(3): sym=syms[i] val=digs[i] if sym=="+": t+=int(val) elif sym=="-": t-=int(val) elif sym=="*": t*=int(val) elif sym=="//": t//=int(val) return t def fun2(ls): for x in ls: s = x.replace("J", "11").replace("Q", "12").replace("K", "13").replace("A", "1") if eval1(s) == 24: return x.replace("//", "/") return "NONE" ls = input().split() if "joker" in ls or "JOKER" in ls: print("ERROR") else: fun(ls, "") ls = list(set(l)) print(fun2(ls))