2012年11月20日 星期二

排列組合

請設計一個程式,可以將一組數字、字母或符號進行排列,以得到不同的組合順序,例如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

5 則留言:

  1. Dim strr 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, mycount
    For i = 1 To mycount
    Input #1, strr
    Call rr("")
    Next
    Close #2
    Close #1
    End
    End Sub


    Public Function rr(k As String)
    If Len(k) = Len(strr) Then
    Print #2, k
    Else
    For i = 1 To Len(strr)
    If InStr(k, Mid(strr, i, 1)) = 0 Then rr (k & Mid(strr, i, 1))
    Next
    End If
    End Function

    回覆刪除
    回覆
    1. 可能要先排序喔!輸入:213

      進入rr函數之前 要先排序成:123

      不然輸出後會不符合題意->>輸出說明:輸出不同的組合順序,順序規則如下:請按照字典排序,由左到右,由上到下,由小到大排列。

      刪除
  2. Dim strr 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, mycount
    For i = 1 To mycount
    Input #1, strr
    Call rr("")
    Next
    For ii = 0 To List1.ListCount
    Print #2, List1.List(ii)
    Next
    Close #2
    Close #1
    End
    End Sub


    Public Function rr(k As String)
    If Len(k) = Len(strr) Then
    List1.AddItem k
    Else
    For i = 1 To Len(strr)
    If InStr(k, Mid(strr, i, 1)) = 0 Then rr (k & Mid(strr, i, 1))
    Next
    End If
    End Function

    回覆刪除
  3. Dim n
    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, x
    For i = 1 To x
    Input #1, n
    For j = 1 To Len(n)
    List.AddItem Mid(n, j, 1)
    Next
    n = ""
    For w = 0 To List.ListCount
    n = n & List.List(w)
    Next
    List.Clear
    Call a("")
    Next
    Close #2
    Close #1
    End
    End Sub

    Public Function a(c) As String
    If Len(n) = Len(c) Then
    Print #2, c
    Else
    For i = 1 To Len(n)
    If InStr(c, Mid(n, i, 1)) = 0 Then Call a(c & Mid(n, i, 1))
    Next
    End If
    End Function

    回覆刪除
  4. Dim a() 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 w = 1 To Val(n)
    Input #1, x
    ReDim a(Len(x))
    For j = 1 To Len(x)
    a(j) = Mid(x, j, 1)
    Next j
    For i = 1 To UBound(a)
    For j = i + 1 To UBound(a)
    If a(i) > a(j) Then z = a(i): a(i) = a(j): a(j) = z
    Next j
    Next i
    Call b("")
    Next w
    Close #2
    Close #1
    End
    End Sub

    Sub b(c)
    If Len(c) = UBound(a) Then
    Print #2, c
    Else
    For i = 1 To UBound(a)
    If InStr(c, a(i)) = 0 Then Call b(c & a(i))
    Next i
    End If
    End Sub

    回覆刪除