试题

试题 试卷

logo

题型:单选题 题类:常考题 难易度:普通

【bj】高中信息技术综合库——排序算法及程序实现1

有如下部分程序段:

a(1)="20": a(2)="16": a(3)= "12": a(4)="o": a(5)="k"

For i=1 To 4

    For j=5 To i+1 Step-1

        If a(j)>a(j-1) Then t=a(j):a(j)=a(j-1):a(j-1)=t

    Next j

    List3.AddItem a(i)

Next i

该程序段运行后,列表框List1中显示的内容为(   )

A、12,16,20,o,k B、o,k,20,16 C、o,k,20,16,12 D、20,16
举一反三
编写VB程序,实现如下功能: 单击“排序数组a”按钮Command1时,对已有数组a的数据进行升序排列,并显示在文本框Label1中;单击“显示数组b”按钮Command2时,将升序数组b的数据显示在文本框Label2中;再单击“合并a和b”按钮Command3时,对数组a和数组b升序合并到字符串中,将合并后的数据在文本框Label3中显示。(运行效果如图所示)。实现上述功能的VB代码如下, 请在划线处填入合适代码。

Dim a(1 To 6) As Integer  

Dim b(1 To 6) As Integer

Private Sub Command1_Click()     '对数组a中的数据进行排序

Dim i As Integer, j As Integer ,k as integer

a(1) = 53 :a(2) = 18:a(3) = 62:a(4) = 22:a(5) = 6:a(6) = 25

s = ""

For i = 1 To 5

    k = i

    For j = i + 1 To 6

        If a(k) > a(j) Then {#blank#}1{#/blank#}

    Next j

    If i <> k Then

        t = a(k): a(k) = a(i): a(i) = t

    End If

Next i

For i = 1 To 6

    s = s + Str(a(i))

Next i

Label1.Caption = s

End Sub

Private Sub Command2_Click() 

b(1) = 3: b(2) = 8: b(3) = 15: b(4) = 27: b(5) = 38: b(6) = 49

'此处部分代码省略

End Sub

Private Sub Command3_Click()  

'将数组a和b中的数据逐个比较后添加到新的字符串s中,并将s在label3中显示

i = 1: j = 1: s = ""

Do While i <= 6 {#blank#}2{#/blank#}j <= 6           '如果两数组都还有数据未合并

    If  {#blank#}3{#/blank#}Then

        s = s + Str(a(i)) : i = i + 1

    Else

        s = s + Str(b(j)) : j = j + 1

    End If

Loop

Do While i <= 6                            '只有数组a还有数据未合并

    s = s + Str(a(i)) : i = i + 1

Loop

Do While j <= 6                            '只有数组b还有数据未合并

    s = s + Str(b(j)) : j = j + 1

Loop

Label3.Caption = s

End Sub

摘苹果游戏。游戏中的苹果树结了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

返回首页

试题篮