c# - X 的所有可能组合分成 N 个堆栈

标签 c# algorithm math combinatorics

我确信这个问题有一个正式的名称,知道这个名称可能会帮助我找到解决方案,但我不知道,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

Integer Partition in Java

Algorithm for generating integer partitions


谷歌命中:

Integer Partition Algorithm by Jerome Kelleher

Integer Partition Algorithm by Daniel Scocco

Fast Algorithms for Generating Integer Partitions (PDF)(看起来很重)

Stony Brook Algorithm Repository - Partitions

关于c# - X 的所有可能组合分成 N 个堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11019574/

相关文章:

c# - C#中switch语句的多个变量

c# - 使用 C++ 包装器时如何访问 C# .dll 属性

algorithm - Push-relabel算法中的残差图有什么意义?

javascript - 在 paperjs 中围绕中心点绘制 X 度弧?

java - 如何初始化 org.apache.commons.math3.geometry.Vector?

c# - SQL Server 日期从字符串转换

javascript - aspx 按钮防止回发 onclick (c#)

图像清晰度指标

string - 从三角不等式理解 BK 树 : How do we derive the (d-n, d+n) 范围?

math - 从中心点转换的向量