用python求解方程

用python求解方程,python,equation-solving,Python,Equation Solving,有这样一个问题: 1X2X3X4X5X6X7X8X9=1942 X=必须是X、+、-、÷运算符或无运算符(89123可以是等) 如何使用python解决这个问题 谢谢。您可以使用python中的解析器模块 import parser formula = "1 + 2 + 3 + 4 + 5 * 6 * 7 * 8 * 9" code = parser.expr(formula).compile() print eval(code) 您可以从以下内容开始: from itertools impo

有这样一个问题:

1
X
2
X
3
X
4
X
5
X
6
X
7
X
8
X
9=1942

X
=必须是X、+、-、÷运算符或无运算符(89123可以是等)

如何使用python解决这个问题


谢谢。

您可以使用python中的
解析器
模块

import parser
formula = "1 + 2 + 3 + 4 + 5 * 6 * 7 * 8 * 9"
code = parser.expr(formula).compile()
print eval(code)

您可以从以下内容开始:

from itertools import product
target = 1942
test_str = "1{0[0]}2{0[1]}3{0[2]}4{0[3]}5{0[4]}6{0[5]}7{0[6]}8{0[7]}9"
for a in product(["*", "", "+", "/", "-", ""], repeat=8): # Iterate all posibilites
  result_str = test_str.format(a)
  if eval(result_str) == target:
    print(result_str)
    break
并进行优化,使其更易于扩展到更多的数字。但对于您的具体问题,这很好。我找到了这个解决方案:

1*2/3+4*56*78/9


如果您需要更多信息,请查看

试一试,首先你会如何着手解决这个问题?当你有了一个想法,把它翻译成Python。当你陷入困境时,你可以在这里寻求帮助。问问题时一定要包括代码。这是来自Euler项目的挑战吗?我不确定@是。我的朋友问我。他面临智力测验的挑战。