我确信这个问题有一个正式的名称,知道这个名称可能会帮助我找到解决方案,但我不知道,Google 的问题措辞一直指向我 Knapsack Problem ,这不是一回事。
我想取一些值 X 并找到将该值拆分为 N 个整数堆栈的所有可能组合。
如果我的措辞令人困惑,这里有一个 X = 4,N = 3 的例子
Stack -> 1 | 2 | 3 |
----------------------
#1-----> 4 | 0 | 0 |
----------------------
#2-----> 3 | 1 | 0 |
----------------------
#3-----> 2 | 1 | 1 |
----------------------
#4-----> 2 | 2 | 0 |
重复是可以接受的,因为它很容易删除,但理想情况下它不会被计算。解决问题的算法将是完美的,但即使找出问题的名称也会使研究更容易。谢谢。
最佳答案
这些其实都是integer partitions作为已删除的回答备注。使用Mathematica :
IntegerPartitions[4, 3] // PadRight //Grid
输出:
4 0 0
3 1 0
2 2 0
2 1 1
我找不到 C# 实现,但这里有几个相关的问题:
Elegant Python code for Integer Partitioning
Algorithm for generating integer partitions
谷歌命中:
Integer Partition Algorithm by Jerome Kelleher
Integer Partition Algorithm by Daniel Scocco
Fast Algorithms for Generating Integer Partitions (PDF)(看起来很重)
关于c# - X 的所有可能组合分成 N 个堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11019574/