当前位置: 高中信息技术 / 综合题
  • 1. (2021高三上·绍兴月考) 某景区渡口游客可乘船往返于河的两岸,有m个人自河东去河西,n个人自河西返回河东。现有一艘船,最大载重量为wt,最多可同时载两人,船自河东出发,最后返回河东。所有人的体重不会超过wt,求把所有人载过河所需最少往返次数。编写一个VB程序,实现上述功能。程序运行时,在列表框List1中显示所有游客的体重和去向(W表示往西、E表示往东),如“132E”表示体重为132往东。单击“统计”按钮Command1,在标签Label1中显示最少往返次数。程序运行界面如图所示。

    1. (1) VB语句List1.AddItem" "中AddItem是 (单选,填字母:A .对象名/B .方法名/ C .属性名)。
    2. (2) 实现上述功能的VB程序如下,请在划线处填入合适代码。

      Const m = 6, n = 8, wt = 200

      Dim d(1 To m + n) As String, a(1 To m + n) As Integer

      Private Sub Form_Load()

          '读取m + n 个人的体重及去向数据存入d 数组,代码略

      End Sub

      Private Sub Command1_Click()

          Dim i As Integer, p As Integer, q As Integer, s As String

          Dim num As Integer, num1 As Integer, num2 As Integer

          '以下代码实现从d 数组中提取每个人的体重数据,根据去向分段存入a 数组

          p = 1 : q =

          For i = 1 To m + n

              s = Mid(d(i), 1, Len(d(i)) - 1)

              If Mid(d(i),  , 1) = "W" Then

                  a(p) = Val(s): p = p + 1

              Else

                  a(q) = Val(s): q = q + 1

              End If

          Next i

          num1 = GetNum(1, m)

          num2 = GetNum(m + 1, m + n)

          If num1 > num2 Then num = num1 Else num = num2

          Label1.Caption = "过河需要最少往返次数:" + Str(num)

      End Sub

      Function GetNum(head As Integer, tail As Integer) As Integer

          Dim i As Integer, j As Integer, k As Integer, cnt As Integer, t As Integer

          '以下代码实现对数组a 降序排序

          i = head

          Do While i < tail

              k = i: i = tail

              For j =

                  If a(j - 1) < a(j) Then

                      t = a(j): a(j) = a(j - 1): a(j - 1) = t

                      i = j

                  End If

              Next j

          Loop

          cnt = 0: i = head: j = tail

          Do While i <= j

              If  Then j = j - 1

              cnt = cnt + 1: i = i + 1

          Loop

          GetNum = cnt

      End Function

    3. (3) 程序加框处的代码有误,请改正。

微信扫码预览、分享更方便