VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Click()
Dim a(9), b(9) As Integer
Dim i, j, k As Integer
Dim temp As Integer
Dim left, right, mid As Integer
Dim str As String
str = ""
'开始产生随机数
Randomize
For i = 0 To 9
a(i) = Int((50 - 20) * Rnd + 20) + 1
b(i) = a(i)
str = str + CStr(a(i)) + " "
Next
Print "随机产生的10个20~50之间的数组元素为:"
Print str
Print
str = ""
'对随机数组进行排序,便于二分法查找
For i = 0 To 8
For j = i + 1 To 9
If a(i) > a(j) Then
temp = a(i)
a(i) = a(j)
a(j) = temp
End If
Next
Next
'开始二分法查找
left = 0: right = 9
Do While left <= right
mid = Int((left + right) / 2)
If a(mid) = 36 Then
For k = 0 To 9
If b(k) = 36 Then
Print "36在随机数组中的位置为:" + CStr((k + 1))
End If
Next
Exit Do
ElseIf a(mid) < 36 Then
left = mid + 1
ElseIf a(mid) > 36 Then
right = mid - 1
End If
Loop
If left > right Then
Print "36不在随机数组中!"
Print
End If
End Sub
评论