|
这个直接定位- Sub pailie4(ParamArray x())'直接定位法
- Dim starttime As Single, endtime As Single
- Dim i As Integer, j As Integer, k As Integer, Num As Long, n As Integer
- Dim ALL(), TEMP1 As Long, TEMP2 As Long
- n = UBound(x) + 1 '元素个数
- starttime = Timer '开始计时
- Num = 1
- For i = 1 To n
- Num = Num * i '递归计算n!
- Next
- For i = 1 To Num
- ReDim ALL(1 To n) '初始化数组all
- ALL(1) = x(0)
- TEMP1 = i
- For j = 2 To n
- TEMP2 = TEMP1 Mod j
- TEMP1 = TEMP1 / j
- If TEMP2 = 0 Then
- ALL(j) = x(j - 1) 'temp2为 0则放在最后
- Else
- For k = j To TEMP2 + 1 Step -1
- ALL(k) = ALL(k - 1) ' temp2之后的元素后移一位
- Next
- ALL(TEMP2) = x(j - 1) 'temp2不为 0 则置于第temp2个元素前
- End If
- Next
- Debug.Print Join(ALL, " ") '输出
- Next
- endtime = Timer
- Debug.Print "共 " & Num & " 种排列!用时 " & endtime - starttime & " 秒!"
- End Sub
复制代码 |
|