'請找出為何以下搜尋程式,輸入1及34找不到,而其他值都沒有問題
Public Class Form1
Dim i
Dim isFound = False
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 sN
Dim a() = {2, 1, 5, 10, 22, 34, 17}
Array.Sort(a)
Dim low = 0
Dim high = UBound(a)
Dim m
While low < high And Not isFound
m = Int((low + high) / 2)
If a(m) = s Then
isFound = True
sN = m
Exit While
Else
If a(m) < s Then
low = m + 1
Else
high = m - 1
End If
End If
End While
If isFound = True Then
MsgBox("找到了在排序後的註標" & sN & "處")
MsgBox("也就是在排序後一般習慣稱的第" & sN + 1 & "個")
Else
MsgBox("未找到")
End If
End Sub
End Class
沒有留言:
張貼留言