题解 | #24点运算# 为啥我这个dfs比直接循环还复杂
24点运算
https://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
card_list = list("A23456789") + ["10"] + list("JQK") def compute(stack, res, solution): if not stack and res == 24: result.append(solution.copy()) return True if stack: # + if compute(stack[1:], res + stack[0], solution + ["+", stack[0]]): return True if stack: # - if compute(stack[1:], res - stack[0], solution + ["-", stack[0]]): return True if stack: # * if compute(stack[1:], res * stack[0], solution + ["*", stack[0]]): return True if stack: # / if compute(stack[1:], res //stack[0], solution + ["/", stack[0]]): return True return False s = input().split() import itertools out = "" if "joker" in s or "JOKER" in s: out = "ERROR" else: pre_nums = [card_list.index(i) + 1 for i in s] for nums in itertools.permutations(pre_nums): result = [] if compute(nums[1:], nums[0], [nums[0]]): temp = [card_list[i - 1] if type(i) == int else i for i in result[0]] out = "".join(temp) break else: out = "NONE" print(out)