2012年7月12日 星期四

排列組合二


從檔案in.txt第一列讀入排列組合的項目n個,再讀入第二列數字m,從n個項目中,取出m個項目的組合,全部輸出到out.txt中。

PS:X1 與 1X 視為相同物
輸入範例:
a b c 1 x
2

輸出範例:
ab
ac
a1
ax
bc
b1
bx
c1
cx
1x

2 則留言:

  1. Dim n As Integer
    Dim a() As String
    Private Sub Form_Load()
    Me.Hide
    Dim ans As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Line Input #1, x
    Input #1, n
    a = Split(x)
    Call abc("", 0)
    Close
    Close
    End
    End Sub
    Sub abc(b, c)
    If Len(b) = n Then
    Print #2, b
    Else
    For i = c To UBound(a)
    If InStr(b, a(i)) = 0 Then Call abc(b & a(i), i + 1)
    Next
    End If
    End Sub

    回覆刪除
  2. Dim x As Integer
    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
    Input #1, x
    a = Split(n)
    Call d("", 0)
    Close #2
    Close #1
    End
    End Sub

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

    回覆刪除