請設計一個程式,可以將一組數字、字母或符號進行排列,以得到不同的組合順序,例如123這三個數的排列組合有:123、132、213、231、312、321六組;或是ABC這三個字母的排列組合有:ABC、ACB、BAC、BCA、CAB、CBA六組。
輸入說明:第一列為要排列的列數,第二列及以後為一組由數字、字母或符號組成的字串。(請參照輸入範例)
輸入範圍:每個數字、字母或符號皆為一個字元,每組最少為3個字元,最多不超過10個,且不重覆。
輸入範例:in.txt
2
213
ABC
輸出說明:輸出不同的組合順序,順序規則如下:請按照字典排序,由左到右,由上到下,由小到大排列。(請參照輸出範例)
輸出範例:out.txt
123
132
213
231
312
321
ABC
ACB
BAC
BCA
CAB
CBA
Dim x 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
Input #1, n
For i = 1 To n
Input #1, x
Call a("")
Next
Close
Close
End
End Sub
Sub a(b)
If Len(b) = Len(x) Then
Print #2, b
Else
For i = 1 To Len(x)
If InStr(b, Mid(x, i, 1)) = 0 Then Call a(b & Mid(x, i, 1))
Next
End If
End Sub
不知道這題能否重複,
回覆刪除如果輸入100呢(?
我是哲哲,可以試看看
回覆刪除叫Bob有空的話幫你檢查你做過的題目哦
如果要100的話輸出
刪除100
010
001
OK 謝哲哲
刪除作者已經移除這則留言。
回覆刪除Dim x As String
回覆刪除Dim s As Integer
Dim rank() As String
Dim p 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
Input #1, n
For i = 1 To Val(n)
Input #1, x
s = 1
Call a("")
For z = 1 To 2
For q = UBound(rank()) To 1 Step -1
If rank(q) < rank(q - 1) Then
p = rank(q - 1)
rank(q - 1) = rank(q)
rank(q) = p
End If
Next q
Next z
For q = 1 To UBound(rank())
Print #2, rank(q)
Next q
Next i
Close #2
Close #1
End
End Sub
Sub a(b)
If Len(b) = Len(x) Then
ReDim Preserve rank(s)
rank(s) = b
s = s + 1
Else
For i = 1 To Len(x)
If InStr(b, Mid(x, i, 1)) = 0 Then Call a(b & Mid(x, i, 1))
Next i
End If
End Sub