排序前 |
55 |
70 |
70 |
78 |
71 |
79 |
75 |
80 |
80 |
90 |
排序后 |
55 |
70 |
70 |
71 |
75 |
78 |
79 |
80 |
80 |
90 |
查找该组数据中满足[k1, k2]范围的数据个数为:8 |
实现上述功能的VB程序如下,请回答下列问题:
Private Sub Command1_Click()
Const n = 10
Dim a(1 To n) As Integer, k As Integer
Dim i As Integer, j As Integer, k1 As Integer, k2 As Integer
Dim p As Integer, q As Integer
Dim b As Integer '查找满足条件的第一个数据所在数组元素的下标
'读取排序前的一组正整数,存储在数组a中。代码略
p= Val(Text1.Text)
q = Val(Text2.Text)
For i = p To q - 1
For j = Step -1
If a(j) < a(j - 1) Then
k = a(j): a(j) = a(j - 1): a(j - 1) = k
End If
Next j
Next i
k1 = Val(Text3.Text)
k2 = Val(Text4.Text)
i = 1: j = n
Do While i <= j
m = (i + j) \ 2
If ① Then
i = m + 1
Else
j = m - 1
End If
Loop
b = ②
i = 1: j = n
Do While i <= j
m = (i + j) \ 2
If a(m) <= k2 Then
i = m + 1
Else
j = m - 1
End If
Loop
Label1.Caption = Str ③
End Sub
① ② ③