我正在尝试编写vb.net代码以返回set的唯一组合 我的集包含3个不同的元素。我发现了类似帖子这篇文章,但找不到任何VB解决方案来获得这个结果
示例:
元素:1,2,3
{1,2,3}
结果必须是
1
2
3
12
13
23
123
...........
>...................
我试图通过使用以下代码来实现这一目标
Function GetCombinations(ByVal depth As Integer, ByVal values As String()) As IEnumerable(Of String)
If depth > values.Count + 1 Then Return New List(Of String)
Dim result = New List(Of String)
For i = 0 To depth - 1
For y = 0 To values.Count - 1
If i = 0 Then
result.Add(values(y))
Else
result.Add(values(i - 1) + values(y))
End If
Next
Next
Return result
End Function
获得结果
Dim reslt = GetCombinations(4, data_array)
?reslt
Count = 12
(0): "1"
(1): "2"
(2): "3"
(3): "11"
(4): "12"
(5): "13"
(6): "21"
(7): "22"
(8): "23"
(9): "31"
(10): "32"
(11): "33"
提示: 我使用数学并设法计算没有组合。我可以测试一下 这个公式
实施例 这个公式叫做nCr。它意味着在n个元素中有多少种方法可以使用r的唯一组合来获取r个元素。
nPr = n!/(n-r)!
n! = 1 * 2 * 3 * 4* ... (n-1) * n
Elements: 1, 2, 3
In this case n = 3 and r can be 1, 2, and 3 all
number of combinations = 3P1 + 3P2 + 3P3
= 3!/2! + 3!/1! + 3!/0!
= 6/2 + 6/1 + 6/1 (0!=1)
= 3+6+6
= 15
答案 0 :(得分:1)
了解该术语可以更轻松地找到现有算法。您正在寻找的是电源设置。这是我在Rosetta Code上找到的C#实现的快速VB.NET翻译:
Public Function GetPowerSet(Of T)(ByVal input As IEnumerable(Of T)) As IEnumerable(Of IEnumerable(Of T))
Dim seed As IEnumerable(Of IEnumerable(Of T)) = {Enumerable.Empty(Of T)()}
Return input.Aggregate(seed, Function(a, b) a.Concat(a.Select(Function(x) x.Concat({b}))))
End Function
测试:
For Each x In GetPowerSet({1, 2, 3})
Console.WriteLine(String.Join(", ", x))
Next
输出:
1
2
1, 2
3
1, 3
2, 3
1, 2, 3
编辑 - 根据您的最新解释,我认为您需要采用不同的方法。对于所有大小达到输入大小,您似乎想要combinations with repetitions / replacement。对于 k 的每个值,您可以简单地使用参数(S,k)调用其中一个算法,从1到 n 并加入所有结果分成一组结果。
Public Iterator Function GetCombinationsWithReplacement(Of T)(source As IEnumerable(Of T), size As Integer) As IEnumerable(Of IEnumerable(Of T))
Dim pool = source.ToList()
Dim n = pool.Count
If n = 0 AndAlso size > 0 Then
Return
End If
Dim indices = Enumerable.Repeat(0, size).ToList()
Yield indices.Select(Function(i) pool.Item(i))
While True
Dim index As Nullable(Of Integer) = Nothing
For i = size - 1 To 0 Step -1
If indices.Item(i) <> n - 1 Then
index = i
Exit For
End If
Next
If Not index.HasValue Then
Return
End If
indices = indices.Take(index.Value).Concat(Enumerable.Repeat(indices.Item(index.Value) + 1, size - index.Value)).ToList()
Yield indices.Select(Function(i) pool.Item(i))
End While
End Function
(如果你的VB.NET编译器不支持Yield,你需要修改它。)
调用不同大小的结果是:
GetCombinationsWithReplacement({1, 2, 3}, 1)
:
{1}
{2}
{3}
GetCombinationsWithReplacement({1, 2, 3}, 2)
:
{1, 1}
{1, 2}
{1, 3}
{2, 2}
{2, 3}
{3, 3}
GetCombinationsWithReplacement({1, 2, 3}, 3)
:
{1, 1, 1}
{1, 1, 2}
{1, 1, 3}
{1, 2, 2}
{1, 2, 3}
{1, 3, 3}
{2, 2, 2}
{2, 2, 3}
{2, 3, 3}
{3, 3, 3}
我们可以将这些加入到包含所有19个子集的单个序列中:
Public Iterator Function GetCombinationsWithReplacementAllSizes(Of T)(source As IEnumerable(Of T)) As IEnumerable(Of IEnumerable(Of T))
Dim pool = source.ToList()
For size = 1 To pool.Count
For Each subset In GetCombinationsWithReplacement(pool, size)
Yield subset
Next
Next
End Function
答案 1 :(得分:0)
这里有一些伪代码可以帮助你(我没有声称这是最快的方法,这只是一种方法。)
给定resultList是结果列表
loop by # items in elementlist
{
if tempList is empty // special case for first iteration
add each element of elementList to tempList and resultlist
else
{
for each element in templist
for each element2 in elementlist
add combo to result list
copy elements added to result list for this iteration to templist
}
}