现在的位置: 首页 > 综合 > 正文

VB.NET 排列组合算法实现

2013年08月03日 ⁄ 综合 ⁄ 共 1269字 ⁄ 字号 评论关闭

 假设给定数组 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

抱歉!评论已关闭.