|
这个是使用递归的,也是返回全排列,看起来应该是比之前那个动态生成代码的要清楚多了,用法都一样~
- Function GetAllPermutation(ByVal sArr)
- sLen=UBound(sArr)+1
- aLen=Factorial(sLen)
- s="Dim aResult("&aLen&")"
- Execute s
- Call GetString(sArr,"",aResult,0)
- GetFullPermutation=aResult
- End Function
-
- Function GetString(ByVal aTemp,ByVal strTemp,ByRef aResult,ByRef counter)
- Dim i,str
- If UBound(aTemp)>0 Then
- For i=0 To UBound(aTemp)
- str=strTemp&aTemp(i)
- Call GetString(RemoveFromArray(aTemp,i),str,aResult,counter)
- Next
- Else
- aResult(counter)=strTemp&aTemp(0)
- counter=counter+1
- End If
- End Function
-
- Function RemoveFromArray(ByVal arr,index)
- Dim sTemp,arrTemp
- arr(index)=""
- sTemp=Join(arr,"|")
- sTemp=Replace(sTemp,"||","|")
- If Left(sTemp,1)="|" Then
- sTemp=Right(sTemp,Len(sTemp)-1)
- ElseIf Right(sTemp,1)="|" Then
- sTemp=Left(sTemp,Len(sTemp)-1)
- End If
- arrTemp=Split(sTemp,"|")
- RemoveFromArray=arrTemp
- End Function
-
- Function Factorial(num)
- Dim n,product
- product=1
- For n=2 To num
- product=product*n
- Next
- Factorial=product
- End Function
复制代码 |
|