展开全部
代码如下:
'arr()为你给定的目标数组,n为你指定的前n个最大值,调用例子:
' Dim arr() As Variant
' arr = Array(1, 4, 5, 6, 2, 6, 8, 4, 6, 7)
' PrintPreN arr, 10
Private Sub PrintPreN(arr() As Variant, n As Long)
Dim Brr() As Variant '排序后的数组
dim Crr() As Variant '用来存储下标
Brr = arr
Dim i, j, tmp, tmp1 As Long
ReDim Crr(UBound(arr) + 1)
For i = 0 To UBound(arr)
Crr(i) = i
Next i
For i = 0 To UBound(Brr) - 1
For j = i + 1 To UBound(arr)
If Brr(i) < Brr(j) Then
tmp = Brr(i)
Brr(i) = Brr(j)
Brr(j) = tmp
tmp1 = Crr(i)
Crr(i) = Crr(j)
Crr(j) = tmp1
End If
Next j
Next i
For i = 1 To n
Print "第" & i & "大的数字为:" & Brr(i - 1), "排序前的下标为:" & Crr(i - 1)
Next i
End Sub
'arr()为你给定的目标数组,n为你指定的前n个最大值,调用例子:
' Dim arr() As Variant
' arr = Array(1, 4, 5, 6, 2, 6, 8, 4, 6, 7)
' PrintPreN arr, 10
Private Sub PrintPreN(arr() As Variant, n As Long)
Dim Brr() As Variant '排序后的数组
dim Crr() As Variant '用来存储下标
Brr = arr
Dim i, j, tmp, tmp1 As Long
ReDim Crr(UBound(arr) + 1)
For i = 0 To UBound(arr)
Crr(i) = i
Next i
For i = 0 To UBound(Brr) - 1
For j = i + 1 To UBound(arr)
If Brr(i) < Brr(j) Then
tmp = Brr(i)
Brr(i) = Brr(j)
Brr(j) = tmp
tmp1 = Crr(i)
Crr(i) = Crr(j)
Crr(j) = tmp1
End If
Next j
Next i
For i = 1 To n
Print "第" & i & "大的数字为:" & Brr(i - 1), "排序前的下标为:" & Crr(i - 1)
Next i
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询