• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

递归与排列

发布: 2007-7-01 21:48 | 作者: admin | 来源: | 查看: 13次 | 进入软件测试论坛讨论

领测软件测试网

笔者曾写过一个递归与组合的算法(),下面给出一个排列的递归算法,请大家指教。

Private Sub Command1_Click() ´ 列出数组a 的全排列
Dim a(8) As String, temp As String
For i = 0 To 8
a(i) = i
Next
temp = permutation(a, UBound(a))
Debug.Print temp
Debug.Print "共有 " & UBound(Split(temp, vbCrLf)) + 1 & " 种排法!"
End Sub
Function addxtostr(ByVal x0 As String, ByVal xadd As String) As String ´ eg: x0: "1,2,3,4",we will add 5 to x0
Dim temp, temp2, all() As String, i As Long
temp = Split(x0, ",")
ReDim all(UBound(temp) + 1)
all(0) = xadd & "," & x0 ´ return "5,1,2,3,4"
For i = 1 To UBound(all)
temp2 = temp
temp2(i - 1) = temp2(i - 1) & "," & xadd ´  add 5 between every two contunious number
all(i) = Join(temp2, ",")
Next
addxtostr = Join(all, vbCrLf)
Set temp = Nothing
Set temp2 = Nothing
Erase all
End Function
Function permutation(ByRef a() As String, ByVal n As Long) As String ´列出数组a 的前n-1 个元素的全排列
Dim i As Long, temp, all() As String
If n = 0 Then permutation = a(0)
If n = 1 Then permutation = a(0) & "," & a(1) & vbCrLf & a(1) & "," & a(0)
If n > 1 Then
temp = Split(permutation(a, n ), vbCrLf) ´ 递归
ReDim all(UBound(temp))
For i = 0 To UBound(temp)
all(i) = addxtostr(temp(i), a(n))
Next
permutation = Join(all, vbCrLf)
End If
Erase all
End Function


文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网