【发布时间】:2015-05-25 03:35:29
【问题描述】:
我只是在 math.stackexchange 上发布了一个数学问题,但我会在这里向人们询问一种编程递归算法。
问题:从1到9填空数字(每个空格一次且仅一次)以完成方程式。
附加条件:
1. Mathematic priority DOES matter.
2. All numbers (include evaluation result) should be integers.
Which mean the divide should be divisible (E.g. 9 mod 3 = 0 is OK, 8 mod 3 != 0 is not OK).
3. For those who don't know (as one in the original question), the operations in the diagram are:
+ = plus; : = divide; X = multiple; - = minus.
应该有超过 1 个答案。我想要一个递归算法来找出所有的解决方案。
PS:我想了解递归算法,性能提升。我试图使用蛮力解决问题。我的电脑冻结了很长一段时间。
【问题讨论】:
-
到目前为止你有什么,你到底在问什么?我希望不是“你能帮我写代码吗?”
-
我只有一个蛮力解决方案。我正在寻找一种快速、易于阅读和开发的算法。我曾想过使用递归,但不知道从哪里开始,从哪里结束,从哪里分支循环。
-
“我试图用蛮力解决这个问题。我的电脑卡了好一阵子。” 奇怪,只有 9 个! = 362,800 种不同的可能性,所以暴力破解应该很快。
-
也许我使用了太多的循环、变量、分支条件?
-
@Eddie 可能有太多循环,请发布您的蛮力解决方案。