發新話題
打印

請問一個關於數學的全排列組合的寫法

excel 版的排列

在教排列組合時, 常常會想要看排列的情形, 3!, 4! 還可以用人工列出來, 5!, 6! 可就麻煩了, 偶然發現以下的Excel VBA , 還不錯, 給大家參考!
http://spreadsheetpage.com/index.php/tip/generating_permutations/
Dim CurrentRow
Sub GetString()
    Dim InString As String
    InString = InputBox("Enter text to permute:")
    If Len(InString) < 2 Then Exit Sub
    If Len(InString) >= 8 Then
        MsgBox "Too many permutations!"
        Exit Sub
    Else
        ActiveSheet.Columns(1).Clear
        CurrentRow = 1
        Call GetPermutation("", InString)
    End If
End Sub

Sub GetPermutation(x As String, y As String)
'   The source of this algorithm is unknown
    Dim i As Integer, j As Integer
    j = Len(y)
    If j < 2 Then
        Cells(CurrentRow, 1) = x & y
        CurrentRow = CurrentRow + 1
    Else
        For i = 1 To j
            Call GetPermutation(x + Mid(y, i, 1), _
            Left(y, i - 1) + Right(y, j - i))
        Next
    End If
End Sub

[ 本帖最後由 荷荷葩 於 2014-9-10 11:11 PM 編輯 ]

TOP

發新話題