求四个不同数字所有排列可能的print方法。。

billylk 2004-05-01 08:12:18
老师布置的。。。
是为做算24点做准备。。。

要求就是我的标题。。
用循环语句。。。

我刚学不久。。
所以。。。菜。。。呵呵。。。。
...全文
160 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
liwowo 2004-06-08
  • 打赏
  • 举报
回复
下面给出一个排列的递归算法,请大家指教。

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
northwolves 2004-05-02
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2909/2909080.xml?temp=.2501337

741

社区成员

发帖
与我相关
我的任务
社区描述
VB 版八卦、闲侃,联络感情地盘,禁广告帖、作业帖
社区管理员
  • 非技术类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧