Option Explicit 'Must Declare all variables,
'I think that it is good practice
Option Base 1 'For simplicity,
'make array index start at 1 To n
Sub Perm(A() As String, m, n As Integer)
Dim i As Integer
Dim Temp As String
If m = 1 Then
PrintPerm A, n 'Print out the permutations
Else
For i = 1 To m
Temp = A(i)
A(i) = A(m) 'Exchange A(i) and A(m), could
'have made a Sub To Do this
A(m) = Temp
Perm A, m - 1, n 'Recursive Function call
Temp = A(m)
A(m) = A(i) 'Exchange A(m) and A(i)
A(i) = Temp
Next i
End If
End Sub
Private Sub Command1_Click()
Dim i As Integer
n = Len(Text1.Text)
ReDim strArray(n) As String
For i = 1 To n
strArray(i) = Mid(Text1.Text, i, 1)
Next i
' strArray(1) = "a"
' strArray(2) = "b" 'Initialize array With abcdcfg
' strArray(3) = "c" 'Create a text box To input other strings
' strArray(4) = "d"
' strArray(5) = "e"
' strArray(6) = "f"
' strArray(7) = "g"
Perm strArray(), m, n 'Of course you could use variables For this
Print n & "份資料排列" & n & "! = "; F(n)
End Sub
Public Sub PrintPerm(A() As String, n As Integer)
Dim i As Integer
Dim strVar As String
For i = 1 To n
strVar = strVar & (A(i))
Next i
List1.AddItem strVar
End Sub
Function F(n)
If n = 0 Then ' 當 N=0 時,F(N) = 1
F = 1
Else
F = n * F(n - 1) ' 當 N>0 時,F(N) = N*F(N-1)
End If ' 以上的 F(N-1) 即為遞迴呼叫
End Function