在 VB .NET 中生成所有实数组合 [英] Generate all real combinations in VB .NET
问题描述
我需要在 VB .NET 中生成所有组合(不是排列),我一直在搜索,我发现的只是排列(有些说是组合,但当我尝试时,所有都是排列).
I need to generate all combinations (not permutations) in VB .NET, I've been search and all I found is for permutations (some says combinations but when I was try it, all are permutations).
我需要的是从字符串数组生成组合:
What I need is to generate combinations from string array:
Dim data_array As String() = {"one", "two", "three", "four", "five", "six"}
我需要:
如果我只说 1 个长度,它必须返回:
If I say just 1 length, it must returns:
one
two
three
four
five
six
如果我说 2 长度,它必须返回:
If I say 2 length, it must returs:
oneone
onetwo
onethree
onefour
... etc ...
twoone
twotwo
twothree
... etc ...
并继续直到所有组合结束.
And continues til end with all combinations.
如果我说更长,也可以.
If I say more length do it as well.
推荐答案
你说你想生成所有的组合,但在我看来,它想要生成所有的排列.
You say you want to generate all combinations but it looks to me like to are trying to generate all permutations.
如果您愿意,您可以尝试递归执行.如果您只想生成组合,请在追加之前测试 Root 参数以查看它是否包含 myStr.
You might try to do it recursively if you wish. If you just want to generate combinations then test the Root parameter to see if it contains myStr before appending it.
Public Class Form1
Dim data_array As String() = {"one", "two", "three", "four", "five", "six"}
Dim buffer As New List(Of String)
Public Sub Permute(ByVal Root As String, ByVal Depth As Integer, ByVal Buffer As List(Of String))
For Each myStr As String In data_array
If Depth <= 1 Then
Buffer.Add(Root + myStr)
Else
Permute(Root + myStr, Depth - 1, Buffer)
End If
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Permute("", 2, buffer)
End Sub
End Class
这篇关于在 VB .NET 中生成所有实数组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!