求E语言数字全排列的算法,用纯代码将一串数字进行大小排列

飘零雨中的红叶2022-10-04 11:39:541条回答

已提交,审核后显示!提交回复

共1条回复
wbdlcn 共回答了18个问题 | 采纳率88.9%
534216789.
程序代码如下:
Private Sub Degr(p() As Integer,ByVal n As Integer)
Dim i As Integer,j As Integer
Do While n > 0
OutL p
If p(1) = n Then '如果第一位是n
i = 0
Do '从左端开始找出最长的连续递降序列
i = i + 1
If i = n Then Exit Sub
Loop Until p(i) p(i + 1) + 1
j = i
Do '找出递降序列末尾数字的下一个数字
i = i + 1
Loop Until p(i) = p(j) - 1
Swap p(i),p(i - 1) '将它与序列末尾数字交换
For i = 1 To n - j '将递减序列倒转后放置在排列右端
p(i) = p(i + j)
Next
For i = 1 To j
p(n - i + 1) = n - i + 1
Next
Else '如果最高位不是n
i = 0 '从左端开始
Do '找出n所在位置
i = i + 1
Loop Until p(i) = n
Swap p(i),p(i - 1)'将n与其左边数字交换
End If
Loop
End Su
1年前

相关推荐