Dim Y() As String Private Sub Form_Load() Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Do While Not EOF(1) Line Input #1, X Y = Split(X) Call ABC("") Loop Close #2 Close #1 End End Sub
Sub ABC(A) If Len(A) = (UBound(Y) + 1) Then Print #2, A Else For I = 0 To UBound(Y) If InStr(A, Y(I)) = 0 Then Call ABC(A & Y(I)) Next I End If End Sub
----in.txt------- a b c 1 ----out.txt------- abc1 ab1c acb1 ac1b a1bc a1cb bac1 ba1c bca1 bc1a b1ac b1ca cab1 ca1b cba1 cb1a c1ab c1ba 1abc 1acb 1bac 1bca 1cab 1cba
現在覺得遞迴不難了。
回覆刪除Dim Y() As String
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do While Not EOF(1)
Line Input #1, X
Y = Split(X)
Call ABC("")
Loop
Close #2
Close #1
End
End Sub
Sub ABC(A)
If Len(A) = (UBound(Y) + 1) Then
Print #2, A
Else
For I = 0 To UBound(Y)
If InStr(A, Y(I)) = 0 Then Call ABC(A & Y(I))
Next I
End If
End Sub
----in.txt-------
a b c 1
----out.txt-------
abc1
ab1c
acb1
ac1b
a1bc
a1cb
bac1
ba1c
bca1
bc1a
b1ac
b1ca
cab1
ca1b
cba1
cb1a
c1ab
c1ba
1abc
1acb
1bac
1bca
1cab
1cba
佑好,
回覆刪除程式正確,熟能生巧。
Dim n As String, s
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
s = Split(n)
Call Fun("")
Close
Close
End
End Sub
Function Fun(a As String)
If Len(a) = 4 Then
Print #2, a
Else
For i = 0 To 3
If InStr(a, s(i)) = 0 Then Call Fun(a & s(i))
Next
End If
End Function