VB.NET 排列组合算法实现

本文介绍了一种用于生成给定数组中所有可能组合的算法,并提供了VB.NET和C#的实现代码。该算法能够处理任意数量的不重复元素,通过递归方式枚举所有可能的组合形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

假设给定数组 A[ ] , A中元素不重复,要求出所有元素的组合:

A[a, b, c]

→ a

→ b

→ c

→ a b

→ a c

→ b c

→ a b c

' 求给定数组,n个元素进行组合的枚举结果。 ' n=1时,组合个数为1,如:a; ' n=2时,组合个数为2,如:ab; ' n=3时,组合个数为3,如:abc; Function Comb(ByVal arr As List(Of String), ByVal n As Integer) As List(Of String) Dim newArray As List(Of String) = New List(Of String) If n = 1 Then newArray = arr ElseIf n = 2 Then For i As Integer = 0 To arr.Count - 1 For j As Integer = i + 1 To arr.Count - 1 newArray.Add(arr(i) & "&" & arr(j)) Next Next Else Dim str As String = "" For i As Integer = 0 To arr.Count - 1 str = arr(i) Dim tempArr As List(Of String) = New List(Of String) For j As Integer = i + 1 To arr.Count - 1 tempArr.Add(arr(j)) Next If tempArr.Count < n - 1 Then Exit For End If Dim arr1 As List(Of String) = Comb(tempArr, n - 1) For Each strTemp As String In arr1 newArray.Add(str & "&" & strTemp) Next Next End If Return newArray End Function

使用:

Sub Main() '使用例: '构造一个数组: Dim arrList As List(Of String) = New List(Of String) From {"a", "b", "c"} For i As Integer = 1 To arrList.Count Dim lstResult As List(Of String) = Comb(arrList, i) For Each str As String In lstResult Console.WriteLine(str) Next Next Console.Read() End Sub
数组:"a", "b", "c", "d"

输出结果:

a
b
c
d
a&b
a&c
a&d
b&c
b&d
c&d
a&b&c
a&b&d
a&c&d
b&c&d
a&b&c&d

C# 版本的:

public static IEnumerable<string> Comb(string chars) { var data = chars.ToCharArray(); var result = new[] {""}; for(var i=0; i<chars.Length; i++) { result = (from a in data from b in result where !b.Contains(a) select a + "" + b).ToArray(); } return result; }输入: abcd
输出:

abc
acb
bac
bca
cab
cba

20200420使用VB6.0求12选3的全组合数 最近需要求组合数(买双色球生成号码用^_) 以前在大学专门学习过VB6.0,今后会迁移到VC6.0。 预计会在2020.5.1强化一下MFC,就可以了! Option Explicit Private Sub Command1_Click() End Sub Private Sub Command2_Click() Dim h1%, h2%, h3%, h4%, h5%, h6% Dim h6sum% Dim i% Dim j% Dim temp% Dim count% Dim guangyayuan(35) As Integer Dim wutongshan(35) As Integer Dim guangyayuan6(35) As Integer Dim hong1%, hong2%, hong3%, hong4%, hong5%, hong6% Dim strWj As String For hong1 = 1 To 10 'For hong2 = hong1 To 11 For hong2 = hong1 + 1 To 11 For hong3 = hong2 + 1 To 12 'Text2 = Text2 & hong1 & "," & hong2 & "," & hong3 & "," & hong4 & vbCrLf Text2 = Text2 & hong1 & "," & hong2 & "," & hong3 & vbCrLf Next hong3 Next hong2 Next hong1 Close #2 'Open "d:\ScoreW2.txt" For Output As #1 Open "d:\Çó12Ñ¡3µÄÈ«×éºÏÊý.txt" For Output As #1 Write #1, Text2 'Write #1, strWj Close #1 End Sub 使用VB6.0求12选3的全组合数: "1,2,3 1,2,4 1,2,5 1,2,6 1,2,7 1,2,8 1,2,9 1,2,10 1,2,11 1,2,12 1,3,4 1,3,5 1,3,6 1,3,7 1,3,8 1,3,9 1,3,10 1,3,11 1,3,12 1,4,5 1,4,6 1,4,7 1,4,8 1,4,9 1,4,10 1,4,11 1,4,12 1,5,6 1,5,7 1,5,8 1,5,9 1,5,10 1,5,11 1,5,12 1,6,7 1,6,8 1,6,9 1,6,10 1,6,11 1,6,12 1,7,8 1,7,9 1,7,10 1,7,11 1,7,12 1,8,9 1,8,10 1,8,11 1,8,12 1,9,10 1,9,11 1,9,12 1,10,11 1,10,12 1,11,12 2,3,4 2,3,5 2,3,6 2,3,7 2,3,8 2,3,9 2,3,10 2,3,11 2,3,12 2,4,5 2,4,6 2,4,7 2,4,8 2,4,9 2,4,10 2,4,11 2,4,12 2,5,6 2,5,7 2,5,8 2,5,9 2,5,10 2,5,11 2,5,12 2,6,7 2,6,8 2,6,9 2,6,10 2,6,11 2,6,12 2,7,8 2,7,9 2,7,10 2,7,11 2,7,12 2,8,9 2,8,10 2,8,11 2,8,12 2,9,10 2,9,11 2,9,12 2,10,11 2,10,12 2,11,12 3,4,5 3,4,6 3,4,7 3,4,8 3,4,9 3,4,10 3,4,11 3,4,12 3,5,6 3,5,7 3,5,8 3,5,9 3,5,10 3,5,11 3,5,12 3,6,7 3,6,8 3,6,9 3,6,10 3,6,11 3,6,12 3,7,8 3,7,9 3,7,10 3,7,11 3,7,12 3,8,9 3,8,10 3,8,11 3,8,12 3,9,10 3,9,11 3,9,12 3,10,11 3,10,12 3,11,12 4,5,6 4,5,7 4,5,8 4,5,9 4,5,10 4,5,11 4,5,12 4,6,7 4,6,8 4,6,9 4,6,10 4,6,11 4,6,12 4,7,8 4,7,9 4,7,10 4,7,11 4,7,12 4,8,9 4,8,10 4,8,11 4,8,12 4,9,10 4,9,11 4,9,12 4,10,11 4,10,12 4,11,12 5,6,7 5,6,8 5,6,9 5,6,10 5,6,11 5,6,12 5,7,8 5,7,9 5,7,10 5,7,11 5,7,12 5,8,9 5,8,10 5,8,11 5,8,12 5,9,10 5,9,11 5,9,12 5,10,11 5,10,12 5,11,12 6,7,8 6,7,9 6,7,10 6,7,11 6,7,12 6,8,9 6,8,10 6,8,11 6,8,12 6,9,10 6,9,11 6,9,12 6,10,11 6,10,12 6,11,12 7,8,9 7,8,10 7,8,11 7,8,12 7,9,10 7,9,11 7,9,12 7,10,11 7,10,12 7,11,12 8,9,10 8,9,11 8,9,12 8,10,11 8,10,12 8,11,12 9,10,11 9,10,12 9,11,12 10,11,12 "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值