Generate all real combinations in VB .NET

后端 未结 2 1620
鱼传尺愫
鱼传尺愫 2021-01-19 09:56

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 a

2条回答
  •  醉话见心
    2021-01-19 10:41

    You say you want to generate all combinations but it looks to me like to are trying to generate all permutations.

    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
    

提交回复
热议问题