2012年11月21日 星期三

排列組合二


從檔案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 num As Integer
    Dim strr
    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, strr
    strr = Split(strr, " ")
    Input #1, num
    Call rr("", 0)
    Close #2
    Close #1
    End
    End Sub

    Public Function rr(k As String, numm As Integer)
    If Len(k) = num Then
    Print #2, k
    Else
    For i = numm To UBound(strr)
    rr = rr(k & strr(i), i + 1)
    Next
    End If
    End Function

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

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

    回覆刪除