跳至内容

算法/查找最大值/visual basic 方法 2

来自维基教科书,一个面向开放世界的开放书籍
Public Function Divide_n_Conquer(ByVal MyNums() As Integer) As Integer

    If myNums.Length = 0 Then
        Return 0
    End If
    If myNums.Length = 1 Then
        Return myNums(0)
    End If
    If myNums.Length = 2 Then
        If myNums(0) > myNums(1) Then
            Return myNums(0)
        Else
            Return myNums(1)
        End If
    End If

    Dim aryA(myNums.Length / 2) As Integer
    Dim aryB(myNums.Length - aryA.Length) As Integer

    Dim i As Integer
    Dim loopindex As Integer = 0

    For i = 0 To myNums.Length / 2
        aryA(i) = myNums(i)
        loopindex += 1
    Next

    Dim intK As Integer = 0

    For i = aryA.Length To myNums.Length - 1
        aryB(intK) = myNums(loopindex)
        loopindex = loopindex + 1
        intK = intK + 1
    Next

    Dim intM As Integer = 0
    Dim intN As Integer = 0

    intM = FindMax(aryA)
    intN = FindMax(aryB)

    If intM > intN Then
        Return intM
    Elselen(a)
        Return intN
    End If

End Function
华夏公益教科书