当前位置: 高中信息技术 / 综合题
  • 1. (2019高三下·浙江月考) 本程序的功能是在列表框List1中随机产生n(n为偶数)个[1,20]之间的不重复正整数,存入数组a中。点击“排序”按钮Command1,对n个数进行排序。使得奇数位上是降序,偶数位上是升序,且a(1)<a(2),并在List2中显示排序后的结果。实现该功能的VB程序如下,但加框处代码有错,请改正。

    Const n = 8

    Dim a(1 To n) As Integer, b(1 To n) As Integer

    Private Sub Form_Load()

        ‘随机产生n个[1,20]之间的不重复的正整数,并在list1中显示,具体代码略

    End Sub

    Private Sub Command1_Click()

        Dim i As Integer, j As Integer, t As Integer, p as integer, p as integer

        For i = 1 To n - 1

            For j = n To i + 1 Step -1

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

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

                End If

            Next j

        Next i

        p = n:    ‘①

        For i = 1 To n Step 2

                ‘②

            b(i + 1) = a(q)

            p = p - 2

            q = q + 1

        Next i

        For i = 1 To n

            List2.AddItem Str(b(i))

        Next i

    End Sub

     ② 

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