2012年4月20日 星期五

排列組合一

從檔案 in.txt 讀入排列組合的項目,將所有可能的排列輸出到out.txt。

輸入範例:
a b c 1

輸出範例:

abc1
bac1
cab1
1abc
acb1
a1bc
bca1
b1ac
cba1
c1ab
1bac
1cab
ab1c
ba1c
ca1b
1acb
ac1b
a1cb
bc1a
b1ca
cb1a
c1ba
1bca
1cba

3 則留言:

  1. 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
    Do While Not EOF(1)
    Line Input #1, n
    a = Split(n)
    Call b("")
    Loop
    Close
    Close
    End
    End Sub
    Sub b(x)
    If Len(x) = (UBound(a) + 1) Then
    Print #2, x
    Else
    For i = 0 To (UBound(a))
    If InStr(x, a(i)) = 0 Then Call b(x & a(i))
    Next
    End If
    End Sub

    回覆刪除
  2. 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
    Line Input #1, n
    a = Split(n)
    Call s("")
    Close #2
    Close #1
    End
    End Sub
    Sub s(x)
    If Len(x) = (UBound(a) + 1) Then
    Print #2, x
    Else
    For i = 0 To UBound(a)
    If InStr(x, a(i)) = 0 Then Call s(x & a(i))
    Next i
    End If
    End Sub

    回覆刪除
  3. 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
    Line Input #1, x
    a = Split(x)
    Call c("")
    Close #2
    Close #1
    End
    End Sub

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

    回覆刪除