Problem 6 (排列組合 17%)
請設計一個程式,可以將一組數字、字母或符號進行排列,以得到不同的組合順序,例如123這三個數的排列組合有:123、132、213、231、312、321六組;或是ABC這三個字母的排列組合有:ABC、ACB、BAC、BCA、CAB、CBA六組。
輸入說明:第一列為要排列的列數,第二列及以後為一組由數字、字母或符號組成的字串。(請參照輸入範例)
輸入範圍:每個數字、字母或符號皆為一個字元,每組最少為3個字元,最多不超過10個,且不重覆。
輸入範例:test6.txt
2
213
ABC
輸出說明:輸出不同的組合順序,順序規則如下:請按照字典排序,由左到右,由上到下,由小到大排列。(請參照輸出範例)
輸出範例:result6.txt
123
132
213
231
312
321
ABC
ACB
BAC
BCA
CAB
CBA
Dim 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, t
For i = 1 To t
List1.Clear
Input #1, n
Call A0(n)
Call A1("", Len(n), 0)
Call A2
Next
Close
Close
End
End Sub
Sub A1(a, b, c)
If c = b Then
List1.AddItem a
Else
For i = 1 To b
If InStr(a, s(i)) = 0 Then Call A1(a & s(i), b, c + 1)
Next
End If
End Sub
Sub A0(a)
ReDim s(Len(a))
For i = 1 To Len(a)
m = Mid(a, i, 1)
s(i) = m
Next
End Sub
Sub A2()
For i = 0 To List1.ListCount - 1
Print #2, List1.List(i)
Next
End Sub
給list排序
回覆刪除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
For i = 1 To N
Input #1, X
List1.Clear
Call ABC("", X, Len(X))
For j = 0 To List1.ListCount - 1
Print #2, List1.List(j)
Next j
Next i
Close #2
Close #1
End
End Sub
Sub ABC(A, B, L)
If Len(A) = L Then
List1.AddItem A
Else
For i = 1 To L
If InStr(A, Mid(B, i, 1)) = 0 Then Call ABC(A & Mid(B, i, 1), B, L)
Next i
End If
End Sub