Vb.net 集合的唯一组合
我正在尝试编写vb.net代码来返回集合的唯一组合 我的集合包含3个不同的元素。我在这篇文章中找到了类似的帖子,但找不到任何VB解决方案来得到这个结果 例如: 要素:1、2、3 {1,2,3} 结果必须是 i、 我试图通过使用以下代码来实现这一点Vb.net 集合的唯一组合,vb.net,Vb.net,我正在尝试编写vb.net代码来返回集合的唯一组合 我的集合包含3个不同的元素。我在这篇文章中找到了类似的帖子,但找不到任何VB解决方案来得到这个结果 例如: 要素:1、2、3 {1,2,3} 结果必须是 i、 我试图通过使用以下代码来实现这一点 Function GetCombinations(ByVal depth As Integer, ByVal values As String()) As IEnumerable(Of String) If depth > values.
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"
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
}
}
提示:
我学习数学,并设法计算出组合的数量。我可以用它来测试这个系统
这个公式
范例
有一个公式叫做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
这里有一些用于任意数量元素的伪代码可以帮助您(我不认为这是实现这一点的最快方法,它只是实现这一点的一种方法。)
- 给定元素列表是给定元素的列表
- 给圣堂武士的是一份临时持有者名单
- 给定的resultList是一个结果列表
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"
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 } }
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
编辑-根据您最近的解释,我认为您需要一种不同的方法。似乎您希望,对于输入大小的所有大小。您可以简单地调用其中一个算法,为k的每个值(从1到n)调用参数(S,k),并将所有结果合并到一组结果中
翻译:
GetCombinationsWithReplacement({1,2,3},2)
:
GetCombinationsWithReplacement({1,2,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
输出是否必须像示例输出中那样进行排序?请显示您编写了哪些代码来尝试此操作。只有当你花时间编写一个解决方案时,帮助你才会有成效。以二进制形式写出0到7之间的整数:000001010 011100101110111。现在,将低二进制数乘以第一个数,将第二个二进制数乘以第二个数,将高二进制数乘以第三个数。那是000,001,020,021,300,301,320,321。现在,您看到这个过程如何让您获得解决方案了吗?Az Za-我已经编辑过,添加代码是1,3和3,1不同的组合?理论上,输出必须有15个唯一的组合,但通过使用您的代码,我得到了一些数字-->121、231、32、31、2,3@user1403070嗯,在你的帖子上的评论中,你说{1,3}和{3,1}是一样的。如果不应包括顺序不同的相同项目,则这7项是唯一的输出。否则你需要排列,我真的很感激,但是仍然有一些独特的组合缺少{1,1},{2,2},{3,3},{1,1,1},{2,2,2},{3,3},{1,1,2},{1,1,3},{1,2,1},{1,3,1},{1,2},{1,2},我认为它比15@user1403070我不确定你现在想要什么,因为包括{1,1,3}和{1,3,1}似乎与你的规则{1,3}和{3,1}相矛盾都是一样的。另外,你原来的问题是集合,一个集合根据定义没有重复项,也没有顺序——换句话说,所有这些都被认为是同一个集合:{1,2},{2,1},{1,2,1},{1,2,2},你纠正了我之前评论中的错误。缺少的组合是{1,1}、{2,2}、{3,3}、{1,1,1}、{2,2,2}、{3,3,3}、{1,1,2}、{1,1,3}……对此表示抱歉。
{1}
{2}
{3}
{1, 1}
{1, 2}
{1, 3}
{2, 2}
{2, 3}
{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}
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