摘苹果游戏。游戏中的苹果树结了n个苹果,每个苹果有一个地面高度和摘它所需要的力气,要摘到苹果,必须具备高度和力气两个条件,每摘一个苹果都要用掉一定的力气。小林的可用力气是个有限值s,小林手伸直后能摘的最大高度为b,她可以借助的梯子的高度为a。游戏中苹果的高度和所需力气分别存储在数组 h和数组c中。程序运行界面如图所示。运行程序后,输入梯子高度a、手伸直高度b、可用力气s的值, 单击“计算”按钮(Command1),在文本框Text4中输出小林最多能摘得的苹果数ans。
相应程序如下,在划线处填入适当的语句和代码,把程序补充完整。Dim c(1 To 100) As Integer, h(1 To 100) As Integer, d(1 To 100) As Integer
Dim n As Integer
Private Sub Form_Load()
‘从数据库中读取n个苹果的摘取所需力气和高度存放在数组c和h中,并显示在List1中,代码略
End Sub
Private Sub Command1_Click()
Dim a As Integer, b As Integer, s As Integer, i As Integer, j As Integer, m As Integer
a = Val(Text1.Text) ‘梯子高度
b = Val(Text2.Text) ‘手伸直高度
s = Val(Text3.Text) ‘可用力气
m = 0
For i = 1 To n ‘将所有能够摘得的苹果所需力气存储到数组d中
If{#blank#}1{#/blank#}Then
m = m + 1
d(m) = c(i)
End If
Next
For i = 1 To m - 1
k = i
For j = i + 1 To m
If{#blank#}2{#/blank#}Then k = j
Next
If k <> i Then
t = d(k): d(k) = d(i): d(i) = t
End If
Next
ans = 0 ‘用剩余的力气去摘最多的苹果
For i = 1 To m
If s >= d(i) Then
{#blank#}3{#/blank#}
ans = ans + 1
End If
Next
Text4.Text = Str(ans)
End Sub