假設有3個不同的餅乾,要分給3給小朋友,
每人一個所有可能的分法共有(1,2,3),
(1,3,2),(3,2,1),(3,1,2),(2,1,3),
(2,3,1).....六種.
這個程式的問題出在哪?
Dim a() As Integer, n As Integer, c As Integer
Private Sub Command1_Click()
Form1.Cls
c = 0
n = InputBox("輸入一個數字", "", "3")
ReDim a(n)
For i = 1 To n
a(i) = i
Next
Form1.Print
Call p(1)
End Sub
Public Sub p(ByVal k As Integer)
Static x As Integer
x = x + 1
If k = n Then
c = c + 1
Form1.Print c; ": ";
Call display
Form1.Print
Else
For i = k To n
t = a(k)
a(k) = a(i)
a(i) = t
Call p(k + 1)
Next
End If
End Sub
Public Sub display()
For y = 1 To n
Form1.Print a(y);
Next
End Sub