计算有效组合的列表
因此,我提出了一个无法在逻辑上解决的问题。我已经充实了一个例子; 您有一个需要输出的数字列表(例如3x数字1&2x数字2) - 我们将其称为'目标号码'。
您还为您提供了一系列源编号(例如2、3、4和5)。
任务是返回源编号的所有有效组合,以使您产生目标编号。您可以使用任何组合和数量的源编号。
约束是您可以将源号分解为目标号(例如,您可以将5分解为2和3),但是您不能将源编号添加在一起以达到目标编号(例如,您可以' t将1添加到1以达到2)。
剩余者是完全可以接受的(例如,使用源3进入目标2,其余1是组合的一部分,而不是“使用”到达目标的一部分)。
为了限制结果的利益,您还希望有一个约束,即可接受的组合不包含任何“完全未使用的”源编号(即,结果既不是分裂的结果,也不是结果中的目标号),
因此在示例目标中&给出的源号,以下结果将是有效的; [1,1,1,2,2],[3,4],[3,3,1],[4,4] 但是[1,1,1,1,1,2]是无效的,因为您不能将两个源1加入到目标2
。递归,但我见过的唯一示例是约束逆转的位置(即您可以将源编号添加在一起以达到目标号,但不能将它们分开以达到一个
)代码排列?
So I came up with a problem that I can’t logically solve. I’ve fleshed out an example;
You have a list of numbers that you need to output (e.g. 3x number 1 & 2x number 2) - we’ll call them ‘target numbers’.
You’re also given a range of source numbers (e.g. 2, 3, 4 and 5).
The task is to return all valid combinations of the source numbers that would allow you to produce the target numbers. You can use any combination and quantity of source numbers.
The constraints are that you can break a source number down to get to target numbers (e.g. you could break down a 5 into a 2 and a 3) but you cannot add source numbers together to get to a target number (for example you can’t add a 1 to a 1 to get to a 2).
Remainders are perfectly acceptable (e.g. using a source 3 to get to a target 2 and the remaining 1 is part of the combination but not ‘used’ in getting to the target).
In the interests of limiting results you’d also want to have a constraint that an acceptable combination does not contain any ‘totally unused’ source numbers [i.e. neither a result of being split nor a target number in the result)
So in the example target & source numbers given, the following results would be valid;
[1,1,1,2,2],[3,4],[3,3,1],[4,4]
But a [1,1,1,1,1,2] would not be valid, because you cannot join two source 1’s together to make a target 2.
I’ve thought about this logic and am largely thinking the solution involves some level of recursion, but the only examples I’ve seen are where the constraints are reversed (i.e. you can add source numbers together to reach a target number, but cannot split them to reach one)
What kind of logic would you use to generate all valid permutations in code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论