有一组一维数组,求其最大值,并求出最大值的位置,即最大值的 
下标,如何实现啊!例如有12345678987654321这些数,求出9的下标! 
给源码吧,谢谢!

解决方案 »

  1.   

    Private Sub Command1_Click()
        Dim a() As String
        Dim i As Integer
        Dim intT As Integer
        Dim intX As Integer
        
        a = Split("23,3,56,27,12,25", ",")
        
        intT = 0
        For i = 0 To UBound(a) - 1
            If Val(a(i)) > intT Then
                intT = a(i)
                intX = i
            End If
        Next i
        
        MsgBox "max is " & intX
                
        
    End Sub
      

  2.   

    Private Sub Command1_Click()
        Dim a As String
        Dim intT As Integer
        Dim intX As Integer
        
        a = "12345678987654321"
        
        For i = 1 To Len(a)
            If Mid(a, i, 1) > intT Then
                intT = Mid(a, i, 1)
                intX = i
            End If
        Next i
        
        MsgBox "max is " & intT & ",location is " & intX
        
        
    End Sub
      

  3.   

    Private Type MType
        value As Variant
        locate As Integer
    End TypePrivate Sub Form_Load()
        Dim a(7) As Integer
        a(0) = 1
        a(1) = 8
        a(2) = 1
        a(3) = 8
        a(4) = 4
        a(5) = 3
        a(6) = 9
        a(7) = 1
        
        Dim m As MType
        m = MaxValue(a)
        MsgBox "最大值是:" & m.value & "     位置是:" & m.locate
    End SubPrivate Function MaxValue(ByVal arr As Variant) As MType
        Dim i As Integer
        Dim max As MType
        max.value = arr(LBound(arr))
        max.locate = LBound(arr)
        
        For i = LBound(arr) To UBound(arr)
            If arr(i) > max.value Then
                max.value = arr(i)
                max.locate = i
            End If
        Next
        MaxValue = max
    End Function
      

  4.   

    1、设你原来的数组是 a(0),a(1)...,再设一个数组 b(0),b(1)...并将a数组的值赋予给b数组
    For i = 0 To UBound(a)
    b(i)=a(i)
    next2、用昌泡排序法找到B()中的最大值
    Dim temp As Integer
    dim i as Integer
    dim j as Integer
    For i = 1 To UBound(b)
      For J = 1 To UBound(b) - i
         If b(J) < b(J + 1) Then
         temp = b(J)
         b(J) = b(J + 1)
         b(J + 1) = temp
         End If
      Next
    Next3、到数组a中找到最大值,并确定位置
    4、删除b
      

  5.   

    这样是不是更简单一些:
    Option Explicit
    Option Base 1
    Private Sub Command1_Click()
            Dim a(), i As Integer, S As Long, T As Long
            a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1)
            S = a(1)
            For i = 2 To UBound(a)
                S = IIf(S >= a(i), S, a(i))
                If S <= a(i) Then T = i
            Next
            Debug.Print "Max(a(" & T & "))=" & S
    End Sub
      

  6.   

    借用一下东方的:    Dim a, i As Integer
        Dim T As Long
        
        a = Array(10, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1)
        
        For i = 1 To UBound(a)
            If a(i) > a(T) Then T = i
        Next
        Debug.Print "Max(a(" & T & "))=" & a(T)