4.
(2020高二下·丽水期中)
某班级学生为毕业晚会的一个节目设计一个仿“V”字造型,先筛选出班级中n名学生的所有男生,然后将参演的男生按照身高,摆出中间低两边高(先右后左)的造型,如图1 所示。王林同学用VB编写模拟“节目造型”程序,功能如下:从数据库中导出所有学生编号、性别和身高数据;单击“筛选排序”按钮Command2,根据身高仿“V”字和造型进行有序排列,并将结果显示在文本框Text2 中。程序运行界面如图2所示。举例说明如下:
原1-7号男生身高
171 172 180 174 176 179 178
筛选排序后序列
171 172 174 176 178 179 180
“造型设计”后序列
180 178 174 171 172 176 179
图1
身高顺序:男3号〉男6号〉男7号〉男5号〉男4号〉男2号〉男1号
图2
Dim h (1 To n) As Integer
Dim height1(1 To n) As Integer
Dim height2(1 To n) As Integer
Dim n As Integer,i As Integer, j As Integer, num As Integer ,tmp As Integer
Dim sex(1 To n) As String
Private Sub Form_Load()
'n名学生的身高和性别由数据库导出,分别存储在数组h 和sex中,代码略!
End Sub
Private Sub Command1_Click()
b = 0
For i = 1 To n 'n名学生中的男生
If sex(i) = “男” Then ① : height1(num) = h(i)
Next i
For i = 1 To num - 1
For j = num To i + 1 Step -1
If height1(j) < height1(j - 1) Then
tmp = height1(j): height1(j) = height1(j - 1): height1(j - 1) = tmp
End If
Next j
Next i
For i = 1 To num
Text1.Text = Text1.Text + " " + Str(height1(i))
Next i
End Sub
Private Sub Command2_Click()
Dim left, right As Integer, i As Integer, mid As Integer
mid = Int((1 + num) / 2)
left = 0: right = 0
height2(mid) = height1(1)
For i = 1 To (num - 1) \ 2
right = right + 1
height2(mid + right) = height1(2*i)
left = left + 1
②
Next i
If num Mod 2 = 0 Then ③
For i = 1 To num
Text2.Text = Text2.Text + " " + Str(height2(i))
Next i
End Sub