2008年12月31日 星期三

二元搜尋 -- 料一甲55王小明

Public Class Form1
    Dim i
    Dim str1 = "未找到"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim s = Val(InputBox("請輸入要找的值?"))
        'Dim s = 22
        Dim a() = {2, 1, 5, 10, 22, 34, 17}
        Array.Sort(a)
        Dim low = 0
        Dim high = UBound(a)
        Dim m = 99999
        While low <= high And s <> m
            m = Int((low + high) / 2)
            If a(m) = s Then
                str1 = "找到了在排序後的註標" & m & "處"
                Exit While
            Else
                If a(m) < s Then
                    low = m + 1
                Else
                    high = m - 1
                End If
            End If
        End While
        MsgBox(str1)
    End Sub
End Class

沒有留言:

張貼留言