比如a(0)=7 a(1)=6 a(2)=7则n=7怎么样用一个最简单效率最高的方式得出这样的结果。谢谢。

解决方案 »

  1.   

    function max(x1 as long,x2 as long)as long
    if x1>x2 then
     max=x1
    else
     max=x2
    end if
    end function调用:dim mMax as long
    mMax=max(max(x1,x2),x3)
      

  2.   

    Option ExplicitPrivate Function MaxItem(ByRef x() As Integer) As Integer
    Dim n As Integer, i As Integer
        For i = LBound(x) To UBound(x)
            If x(i) > n Then n = x(i)
        Next i
        MaxItem = n
    End FunctionPrivate Sub Command1_Click()
    Dim a(2) As Integera(0) = 7
    a(1) = 6
    a(2) = 7MsgBox MaxItem(a)
    End Sub
    很显然,你需要遍历所有的元素,且需要记录其中最大者。实际上,不需要两两比较,因为元素很多时会使逻辑很混乱。
      

  3.   

    实在是没分可用了Dim arry(2)
    arry(0) = 7
    arry(1) = 6
    arry(2) = 7Dim Max As Integer
    Max = 0Dim l
    For l = 0 To UBound(arry)
      If arry(l) > Max Then
         Max = arry(l)
      End If
    NextMsgBox "最大数是=" & Max