我实在想不通哪里开始想不通、我实在

2023-09-11 07:26:21 作者:像雾像雨又像风

通过使用9号这是1到9,你应该找到办法来获得N使用乘法和加法的数量。

By using 9 numbers which are 1 to 9 you should find the number of ways to get N using multiplication and addition.

例如,如果100被给予,你会回答7。

For example, if 100 is given, you would answer 7.

其理由是,有7可能途径。

The reason is that there are 7 possible ways.

100 = 1*2*3*4+5+6+7*8+9

100 = 1*2*3+4+5+6+7+8*9

100 = 1+2+3+4+5+6+7+8*9

100 = 12+3*4+5+6+7*8+9

100 = 1+2*3+4+5+67+8+9

100 = 1*2+34+5+6*7+8+9

100 = 12+34+5*6+7+8+9

如果这个问题就交给你了,你会怎么开始?

If this question is given to you, how would you start?

推荐答案

我们现在可以使用括号?这将通过大量扩大的可能性的数量。

Are we allowed to use parentheses? That would expand the number of possibilities by a lot.

我会尝试找到第一个附加项,让我们说,1×23,第一位。还有那些数量有限,因为我们不能减,我们知道,如果我们得到高于我们的目标的一个术语,我们可以从我们的搜索修剪它。这使得我们在寻找解决方案,23 +的 F = 100,其中的 F 的是完全一样的形式另一个公式。但是,这是完全一样解决原来的问题为数字4-9和目标77!于是打电话给你的递归算法和该子问题添加解决方案,以解决原来的问题。也就是说,如果我们有23 1 + 4,在那里子问题与数字5-9的任何溶液和 N 的= 73?分而治之。

I would try to find the first additive term, let’s say 1×23, first. There are a limited number of those, and since we can’t subtract, we know that if we get a term above our target, we can prune it from our search. That leaves us looking for the solution to 23 + f = 100, where f is another formula of exactly the same form. But that is exactly the same as solving the original problem for numbers 4–9 and target 77! So call your algorithm recursively and add the solutions for that subproblem to the solutions to the original problem. That is, if we have 23 + 4, are there any solutions to the subproblem with numbers 5–9 and n = 73? Divide and conquer.

您可能会受益于部分解决方案的动态表,因为它是可能的,你可能会得到同样的子问题以不同的方式:1 + 2 + 3 = 1×2×3,因此解决与数字4-9和目标的子问题94两次重复的工作。

You might benefit from a dynamic table of partial solutions, since it's possible you might get the same subproblem in different ways: 1+2+3 = 1×2×3, so solving the subproblem with numbers 4–9 and target 94 twice duplicates work.

您可能最好从右到左比由左到右,在最约束的第一原则。 89,8×9,或78 + 9离开可能的解决方案比1 + 2 + 3余地越来越小,1×2×3,12×3,12 + 3或1×23。

You are probably better going from right to left than from left to right, on the principle of most-constrained first. 89, 8×9, or 78+9 leave much less room for possible solutions than 1+2+3, 1×2×3, 12×3, 12+3 or 1×23.