VB.NET如何实现生成所有可能的实际组合?

2026-05-06 10:051阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计436个文字,预计阅读时间需要2分钟。

VB.NET如何实现生成所有可能的实际组合?

在VB.NET中生成所有组合(而非排序)的方法如下:

vbDim data_array As String()={A, B, C, D}Dim combinations As New List(Of String)

Sub GenerateCombinations() Generate(data_array, 0, New List(Of String))End Sub

Private Sub Generate(ByVal array As String(), ByVal index As Integer, ByVal combination As List(Of String)) If index=array.Length Then combinations.Add(String.Join(, combination)) Return End If

' Include the current element combination.Add(array(index)) Generate(array, index + 1, combination)

' Exclude the current element combination.RemoveAt(combination.Count - 1) Generate(array, index + 1, combination)End Sub

我需要在VB .NET中生成所有组合(而不是排列),我一直在搜索,我发现的只是排列(有些人说组合,但是当我尝试它时,所有都是排列).

VB.NET如何实现生成所有可能的实际组合?

我需要的是从字符串数组生成组合:

Dim data_array As String() = {"one", "two", "three", "four", "five", "six"}

我需要它:

如果我只说一个长度,它必须返回:

one two three four five six

如果我说2长度,它必须回复:

oneone onetwo onethree onefour ... etc ... twoone twotwo twothree ... etc ...

并继续使用所有组合结束.

如果我说更多长度也这样做.

你说你想生成所有组合,但它看起来像是试图生成所有排列.

如果您愿意,您可以尝试递归地执行此操作.如果您只想生成组合,那么在附加之前测试Root参数以查看它是否包含myStr.

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

本文共计436个文字,预计阅读时间需要2分钟。

VB.NET如何实现生成所有可能的实际组合?

在VB.NET中生成所有组合(而非排序)的方法如下:

vbDim data_array As String()={A, B, C, D}Dim combinations As New List(Of String)

Sub GenerateCombinations() Generate(data_array, 0, New List(Of String))End Sub

Private Sub Generate(ByVal array As String(), ByVal index As Integer, ByVal combination As List(Of String)) If index=array.Length Then combinations.Add(String.Join(, combination)) Return End If

' Include the current element combination.Add(array(index)) Generate(array, index + 1, combination)

' Exclude the current element combination.RemoveAt(combination.Count - 1) Generate(array, index + 1, combination)End Sub

我需要在VB .NET中生成所有组合(而不是排列),我一直在搜索,我发现的只是排列(有些人说组合,但是当我尝试它时,所有都是排列).

VB.NET如何实现生成所有可能的实际组合?

我需要的是从字符串数组生成组合:

Dim data_array As String() = {"one", "two", "three", "four", "five", "six"}

我需要它:

如果我只说一个长度,它必须返回:

one two three four five six

如果我说2长度,它必须回复:

oneone onetwo onethree onefour ... etc ... twoone twotwo twothree ... etc ...

并继续使用所有组合结束.

如果我说更多长度也这样做.

你说你想生成所有组合,但它看起来像是试图生成所有排列.

如果您愿意,您可以尝试递归地执行此操作.如果您只想生成组合,那么在附加之前测试Root参数以查看它是否包含myStr.

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