import itertools
from itertools import permutations, product
def format_number(num):
"""将数字格式化为整数或浮点数字符串"""
if isinstance(num, float) and num.is_integer():
return str(int(num))
return str(num)
ops = {
'+': lambda x, y: x + y,
'-': lambda x, y: x - y,
'*': lambda x, y: x * y,
'/': lambda x, y: x / y if y != 0 else None
}
def compute_struct1(a, b, c, d, op1, op2, op3):
"""结构: ((a op1 b) op2 c) op3 d"""
step1 = ops[op1](a, b)
if step1 is None:
return None
step2 = ops[op2](step1, c)
if step2 is None:
return None
result = ops[op3](step2, d)
return result
def compute_struct2(a, b, c, d, op1, op2, op3):
"""结构: (a op1 (b op2 c)) op3 d"""
step1 = ops[op2](b, c)
if step1 is None:
return None
step2 = ops[op1](a, step1)
if step2 is None:
return None
result = ops[op3](step2, d)
return result
def compute_struct3(a, b, c, d, op1, op2, op3):
"""结构: a op1 ((b op2 c) op3 d)"""
step1 = ops[op2](b, c)
if step1 is None:
return None
step2 = ops[op3](step1, d)
if step2 is None:
return None
result = ops[op1](a, step2)
return result
def compute_struct4(a, b, c, d, op1, op2, op3):
"""结构: a op1 (b op2 (c op3 d))"""
step1 = ops[op3](c, d)
if step1 is None:
return None
step2 = ops[op2](b, step1)
if step2 is None:
return None
result = ops[op1](a, step2)
return result
def compute_struct5(a, b, c, d, op1, op2, op3):
"""结构: (a op1 b) op2 (c op3 d)"""
step1 = ops[op1](a, b)
step2 = ops[op3](c, d)
if step1 is None or step2 is None:
return None
result = ops[op2](step1, step2)
return result
structures = [
{'compute': compute_struct1, 'format': lambda a, b, c, d, op1, op2, op3: f"(({format_number(a)}{op1}{format_number(b)}){op2}{format_number(c)}){op3}{format_number(d)}"},
{'compute': compute_struct2, 'format': lambda a, b, c, d, op1, op2, op3: f"({format_number(a)}{op1}({format_number(b)}{op2}{format_number(c)})){op3}{format_number(d)}"},
{'compute': compute_struct3, 'format': lambda a, b, c, d, op1, op2, op3: f"{format_number(a)}{op1}(({format_number(b)}{op2}{format_number(c)}){op3}{format_number(d)})"},
{'compute': compute_struct4, 'format': lambda a, b, c, d, op1, op2, op3: f"{format_number(a)}{op1}({format_number(b)}{op2}({format_number(c)}{op3}{format_number(d)}))"},
{'compute': compute_struct5, 'format': lambda a, b, c, d, op1, op2, op3: f"({format_number(a)}{op1}{format_number(b)}){op2}({format_number(c)}{op3}{format_number(d)})"}
]
def solve_24(nums):
"""解24点问题,返回所有可能的解法"""
solutions = set()
for nums_perm in permutations(nums):
a, b, c, d = nums_perm
for ops_comb in product(ops.keys(), repeat=3):
op1, op2, op3 = ops_comb
for struct in structures:
compute_func = struct['compute']
result = compute_func(a, b, c, d, op1, op2, op3)
if result is not None and abs(result - 24) < 1e-6:
expr = struct['format'](a, b, c, d, op1, op2, op3)
solutions.add(expr)
return solutions
if __name__ == "__main__":
nums = list(map(float, input("请输入四个数字(用空格分隔): ").split()))
if len(nums) != 4:
print("请输入恰好四个数字!")
else:
solutions = solve_24(nums)
if solutions:
print("找到以下解法:")
for sol in solutions:
print(sol)
else:
print("无法组成24点。")