當前位置:成語大全網 - 新華字典 - vb中排列組合的輸出函數

vb中排列組合的輸出函數

排列組合很好做,但是如果妳的字符串太長的話。那個真是計算機負荷不起的運算。比如“0123456789”它的不重復的所有組合就有3628800種(算法:10!階乘)。實在是個巨型數字!我自己在電腦上做實驗光是排出這三百萬多個字符順序的組合就需要大約2分鐘,這還不算重復校驗和字符組合的輸出。汗顏!

代碼如下,所有的排列組合生成在妳D盤下的“字符組合.txt”內。

**********************************************************************

Option Explicit

Private Sub Command1_Click()

Dim Num, I, J, Temp, Len1 As Long

Dim FF As Long

Dim Count As Long

Dim str, strTemp, strArr, Arr() As String

'********************************************************************

str = InputBox("請輸入字符串,數字或者字母組成:", "By CoKie")

Len1 = Len(str): ReDim strArr(Len1)

For I = 1 To Len1

strArr(I - 1) = Mid(str, I, 1)

Next

'********************************************************************

Count = 1

For I = 2 To Len1

Count = Count * I

Next

ReDim Arr(Count)

'*********************************************************************

Randomize

For I = 0 To Count - 1

'******************************************

Lab2: strTemp = "": Num = 0

Do While Num < Len1

Lab1: Temp = Int(Rnd * Len1)

If InStr(strTemp, CStr(Temp)) Then GoTo Lab1

strTemp = strTemp & Temp

Num = Num + 1

Loop

'******************************************

For J = 0 To UBound(Arr)

If Arr(J) = strTemp Then GoTo Lab2

Next

Arr(I) = strTemp

Next

'**********************************************************************

FF = FreeFile

Open "D:\字符組合.txt" For Output As #FF

'**************************

For I = 0 To Count - 1

strTemp = ""

For J = 1 To Len1

strTemp = strTemp & strArr(CLng(Mid(Arr(I), J, 1)))

Next

Print #FF, strTemp

Next

'**************************

Close #FF

End Sub