本帖最后由 矢岛舞美 于 2024-9-25 11:16 编辑
1、2、3、4、5、6、7、8、9、10这10个数字中任取4个不同的数,用加减乘除算出24
210种取法,其中13种无解,197种可以算出24
代码+exe成品:https://www.123865.com/s/TKR5Vv-rwK5v
((1 + 2) + 3) * 4
2 * ((3 + 4) + 5)
((3 - 4) + 5) * 6
4 * ((5 - 6) + 7)
((5 + 7) - 8) * 6
(6 * 8) / (9 - 7)
(8 * 9) / (10 - 7)
[Python] 纯文本查看 复制代码 import itertools
import operator
def find_24(nums):
if len(nums) != 4:
raise ValueError("必须输入4个数")
ops = [operator.add, operator.sub, operator.mul, operator.truediv]
op_symbols = ['+', '-', '*', '/']
# 尝试所有数字的排列
for num_permutation in itertools.permutations(nums):
# 尝试所有运算符的排列
for ops_permutation in itertools.product(ops, repeat=3):
# 尝试所有运算符的符号排列
for op_symbols_permutation in itertools.product(op_symbols, repeat=3):
# 尝试所有可能的括号位置
expressions = [
f"(({num_permutation[0]} {op_symbols_permutation[0]} {num_permutation[1]}) {op_symbols_permutation[1]} {num_permutation[2]}) {op_symbols_permutation[2]} {num_permutation[3]}",
f"({num_permutation[0]} {op_symbols_permutation[0]} ({num_permutation[1]} {op_symbols_permutation[1]} {num_permutation[2]})) {op_symbols_permutation[2]} {num_permutation[3]}",
f"({num_permutation[0]} {op_symbols_permutation[0]} {num_permutation[1]}) {op_symbols_permutation[1]} ({num_permutation[2]} {op_symbols_permutation[2]} {num_permutation[3]})",
f"{num_permutation[0]} {op_symbols_permutation[0]} (({num_permutation[1]} {op_symbols_permutation[1]} {num_permutation[2]}) {op_symbols_permutation[2]} {num_permutation[3]})",
f"{num_permutation[0]} {op_symbols_permutation[0]} ({num_permutation[1]} {op_symbols_permutation[1]} ({num_permutation[2]} {op_symbols_permutation[2]} {num_permutation[3]}))"
]
for expr in expressions:
try:
if abs(eval(expr) - 24) < 1e-6:
return expr
except ZeroDivisionError:
continue
return "没有找到能计算出24的方式"
def main():
while True:
try:
# 提示用户输入4个不同的数字
input_str = input("请输入1-10中的4个不同数字,用空格分隔: ")
numbers = list(map(int, input_str.split()))
# 检查输入的数字是否符合要求
if len(numbers) != 4 or any(n < 1 or n > 10 for n in numbers) or len(set(numbers)) != 4:
raise ValueError
# 查找能计算出24的方式
result = find_24(numbers)
print(result)
break
except ValueError:
print("输入无效,请输入1-10中的4个不同数字。")
if __name__ == "__main__":
main()
感觉这样不太方便,优化了两下
添加循环可连续输入版本:
[Python] 纯文本查看 复制代码 import itertools
import operator
def find_24(nums):
if len(nums) != 4:
raise ValueError("必须输入4个数")
ops = [operator.add, operator.sub, operator.mul, operator.truediv]
op_symbols = ['+', '-', '*', '/']
# 尝试所有数字的排列
for num_permutation in itertools.permutations(nums):
# 尝试所有运算符的排列
for ops_permutation in itertools.product(ops, repeat=3):
# 尝试所有运算符的符号排列
for op_symbols_permutation in itertools.product(op_symbols, repeat=3):
# 尝试所有可能的括号位置
expressions = [
f"(({num_permutation[0]} {op_symbols_permutation[0]} {num_permutation[1]}) {op_symbols_permutation[1]} {num_permutation[2]}) {op_symbols_permutation[2]} {num_permutation[3]}",
f"({num_permutation[0]} {op_symbols_permutation[0]} ({num_permutation[1]} {op_symbols_permutation[1]} {num_permutation[2]})) {op_symbols_permutation[2]} {num_permutation[3]}",
f"({num_permutation[0]} {op_symbols_permutation[0]} {num_permutation[1]}) {op_symbols_permutation[1]} ({num_permutation[2]} {op_symbols_permutation[2]} {num_permutation[3]})",
f"{num_permutation[0]} {op_symbols_permutation[0]} (({num_permutation[1]} {op_symbols_permutation[1]} {num_permutation[2]}) {op_symbols_permutation[2]} {num_permutation[3]})",
f"{num_permutation[0]} {op_symbols_permutation[0]} ({num_permutation[1]} {op_symbols_permutation[1]} ({num_permutation[2]} {op_symbols_permutation[2]} {num_permutation[3]}))"
]
for expr in expressions:
try:
if abs(eval(expr) - 24) < 1e-6:
return expr
except ZeroDivisionError:
continue
return "没有找到能计算出24的方式"
def main():
while True:
try:
# 提示用户输入4个不同的数字
input_str = input("请输入1-10中的4个不同数字,用空格分隔: ")
numbers = list(map(int, input_str.split()))
# 检查输入的数字是否符合要求
if len(numbers) != 4 or any(n < 1 or n > 10 for n in numbers) or len(set(numbers)) != 4:
raise ValueError
# 查找能计算出24的方式
result = find_24(numbers)
print(result)
except ValueError:
print("输入无效,请输入1-10中的4个不同数字。")
# 提示用户是否继续
continue_input = input("是否继续输入新的数字进行计算?(y/n): ").strip().lower()
if continue_input != 'y':
break
if __name__ == "__main__":
main()
增加可视化窗口计算版本:
exe成品:https://www.123865.com/s/TKR5Vv-rwK5v
[Python] 纯文本查看 复制代码 import itertools
import operator
import tkinter as tk
from tkinter import messagebox
def find_24(nums):
if len(nums) != 4:
raise ValueError("必须输入4个数")
ops = [operator.add, operator.sub, operator.mul, operator.truediv]
op_symbols = ['+', '-', '*', '/']
# 尝试所有数字的排列
for num_permutation in itertools.permutations(nums):
# 尝试所有运算符的排列
for ops_permutation in itertools.product(ops, repeat=3):
# 尝试所有运算符的符号排列
for op_symbols_permutation in itertools.product(op_symbols, repeat=3):
# 尝试所有可能的括号位置
expressions = [
f"(({num_permutation[0]} {op_symbols_permutation[0]} {num_permutation[1]}) {op_symbols_permutation[1]} {num_permutation[2]}) {op_symbols_permutation[2]} {num_permutation[3]}",
f"({num_permutation[0]} {op_symbols_permutation[0]} ({num_permutation[1]} {op_symbols_permutation[1]} {num_permutation[2]})) {op_symbols_permutation[2]} {num_permutation[3]}",
f"({num_permutation[0]} {op_symbols_permutation[0]} {num_permutation[1]}) {op_symbols_permutation[1]} ({num_permutation[2]} {op_symbols_permutation[2]} {num_permutation[3]})",
f"{num_permutation[0]} {op_symbols_permutation[0]} (({num_permutation[1]} {op_symbols_permutation[1]} {num_permutation[2]}) {op_symbols_permutation[2]} {num_permutation[3]})",
f"{num_permutation[0]} {op_symbols_permutation[0]} ({num_permutation[1]} {op_symbols_permutation[1]} ({num_permutation[2]} {op_symbols_permutation[2]} {num_permutation[3]}))"
]
for expr in expressions:
try:
if abs(eval(expr) - 24) < 1e-6:
return expr
except ZeroDivisionError:
continue
return "没有找到能计算出24的方式"
def calculate(event=None):
try:
# 获取输入的数字
input_str = entry.get()
numbers = list(map(int, input_str.split()))
# 检查输入的数字是否符合要求
if len(numbers) != 4 or any(n < 1 or n > 10 for n in numbers) or len(set(numbers)) != 4:
raise ValueError
# 查找能计算出24的方式
result = find_24(numbers)
result_label.config(text=result)
# 将结果添加到历史记录
history_listbox.insert(tk.END, f"{input_str} -> {result}")
except ValueError:
messagebox.showerror("输入错误", "输入无效,请输入1-10中的4个不同数字。")
# 创建主窗口
root = tk.Tk()
root.title("24点计算器")
# 创建并放置标签、输入框和按钮
instruction_label = tk.Label(root, text="请输入1-10中的4个不同数字,用空格分隔:")
instruction_label.pack()
entry = tk.Entry(root)
entry.pack()
entry.bind("<Return>", calculate) # 绑定 Enter 键到 calculate 函数
calculate_button = tk.Button(root, text="计算", command=calculate)
calculate_button.pack()
result_label = tk.Label(root, text="")
result_label.pack()
# 创建历史记录框
history_label = tk.Label(root, text="历史记录:")
history_label.pack()
history_listbox = tk.Listbox(root, width=50, height=10)
history_listbox.pack()
# 运行主循环
root.mainloop() |