2012年8月22日 星期三

全取排列


題目:


將 N字串 做全取排列,共有幾種方法?
( N字串不超過10位 )


範例:(也把所有組合輸出出來)

輸入: 程式寫寫寫寫程式
輸出:    420 

1 則留言:

  1. Dim a(10), d, x
    Dim c As Boolean
    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, x
    s = 0
    For i = 1 To Len(x)
    b = Mid(x, i, 1)
    a(i) = b & i
    d = d & a(i)
    Next
    Call abc("")
    Print #2, List1.ListCount
    For i = 0 To List1.ListCount
    Print #2, List1.List(i)
    Next
    Loop
    Close
    Close
    End
    End Sub
    Sub abc(t)
    If Len(t) = Len(d) Then
    c = True
    For i = 1 To Len(x)
    t = Replace(t, i, "")
    Next
    For i = 0 To List1.ListCount
    If t = List1.List(i) Then c = False: Exit For
    Next
    If c = True Then List1.AddItem t
    Else
    For i = 1 To Len(x)
    If InStr(t, a(i)) = 0 Then Call abc(t & a(i))
    Next
    End If
    End Sub

    回覆刪除