Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: bifengxia

[分享] [练习] 用递归/循环实现全排列(对比效率)

[复制链接]
 楼主| 发表于 2008-1-30 13:29 | 显示全部楼层

先来一段循环方式的:

' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
'   全排列函数及应用
'   (本函数用循环(列举)实现)
'                     笔锋侠
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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

回复

使用道具 举报

发表于 2008-1-30 21:08 | 显示全部楼层
回复

使用道具 举报

发表于 2008-2-2 20:20 | 显示全部楼层
回复

使用道具 举报

发表于 2011-11-2 21:39 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

发表于 2012-4-9 23:50 | 显示全部楼层
谢楼主分享,支持一下
回复

使用道具 举报

发表于 2012-4-9 23:52 | 显示全部楼层
谢楼主分享,支持一下
回复

使用道具 举报

发表于 2012-4-11 14:12 | 显示全部楼层
什么情况 不明白。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-31 20:19 , Processed in 0.178626 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表