vb中排列组合的输出函数
有1到n,n个数,输出所有排列组合记录在pailie(index,n)数组中。请给出算法,或者直接写出代码。谢谢。...
有1到n,n个数,输出所有排列组合记录在pailie(index,n)数组中。请给出算法,或者直接写出代码。谢谢。
展开
1个回答
展开全部
排列组合很好做,但是如果你的字符串太长的话。那个真是计算机负荷不起的运算。比如“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
代码如下,所有的排列组合生成在你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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询