先来一段循环方式的: ' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ' 全排列函数及应用 ' (本函数用循环(列举)实现) ' 笔锋侠 ' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Function 全排列列举(arr) Dim intCount As Integer, lngMaxRows As Long Dim lngRow As Long, intColumn As Integer Dim intCol As Integer, intIndex As Integer Dim arrTemp() As String intMaxRow = UBound(arr) intMaxRows = intMaxRow + 1 lngMaxRow = intMaxRows ^ intMaxRows - 1 ReDim arrTemp(0 To lngMaxRow, 0 To intMaxRow) For intColumn = 0 To intMaxRow Step 1 intCol = intMaxRow - intColumn '列下标 For lngRow = 0 To lngMaxRow Step 1 intIndex = Int(lngRow / (intMaxRows ^ intColumn)) Mod intMaxRows '行下标 arrTemp(lngRow, intCol) = arr(intIndex) 'ells(lngRow + 1, intCol + 1) = arr(intIndex) '可在函数内输出到工作表 Next lngRow 全排列列举 = arrTemp Next intColumn End Function Sub 全排列测试() Dim arrTestArr() As String Dim arrTemp() As String Dim i As Long, j As Integer ReDim arrTestArr(0 To 2) arrTestArr(0) = "1" arrTestArr(1) = "2" arrTestArr(2) = "a" intMaxRow = UBound(arrTestArr) intMaxRows = intMaxRow + 1 lngMaxRow = intMaxRows ^ intMaxRows - 1 ReDim arrTemp(0 To lngMaxRow, 0 To intMaxRow) arrTemp = 全排列列举(arrTestArr) For i = 0 To lngMaxRow Step 1 For j = 0 To intMaxRow Step 1 Cells(i + 1, j + 1) = arrTemp(i, j) '也可在函数外,通过函数返回值再输出到工作表 Next j Next i End Sub |