|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
求助斑竹
斑竹:
你好!我想在EXCEL做一个001到648的数字的全排列,可就是不懂做,请教您能不能做?
也就是说,在这个全排列里,要求所以的任意的排列组合都有!以前也看过一些高手做过全排列0到6的代码,(如下)
Option Explicit
Sub pailie(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
ActiveSheet.Cells(i, 1) = "'" & Join(ALL, "") '输出
Next
endtime = Timer
MsgBox "共 " & Num & " 种排列!用时 " & endtime - starttime & " 秒!"
End Sub
Sub test()
pailie "0", "1", "2", "3", "4", "5", "6"
End Sub
可就是不懂用,您看能不能帮帮我! |
|