非递归实现不重复序列的全排列(一)

笔者曾写过利用递归实现不重复序列全排列的例子http://blog.csdn.net/northwolves/archive/2004/04/07/19590.aspx

现在给出另一个利用进制实现的代码:

Sub pailie1(ParamArray x())
Dim starttime As Single, endtime As Single
Dim n As Integer, i As Long, Num As Integer, j As Integer
Dim temp1() As String, temp2 As Long, all As New Collection
n = UBound(x) + 1 '元素个数
starttime = Timer '开始计时
ReDim temp1(1 To n)
For i = 0 To n ^ n - 1
temp2 = i
Set all = Nothing
On Error Resume Next
For j = n To 1 Step -1
temp1(j) = x(temp2 Mod n) '转换为n进制
all.Add temp1(j), temp1(j) '防止某一元素重复列出
temp2 = temp2 / n
Next
If all.Count = n Then
Debug.Print Join(temp1, " ") '输出
Num = Num + 1
End If
? DoEvents
Next
endtime = Timer
Debug.Print "共 " & Num & " 种排列!用时 " & endtime - starttime & " 秒!"
End Sub

Private Sub Command1_Click()
pailie1 "a", "b", "c", "d", "e", "f", "g"
End Sub

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值