1. 图图基于排序算法设计了一个数字矩阵排序的VB程序,功能如下:在文本框Text1中输入矩阵的大小n,单击“生成”按钮Command1,在左侧列表框List1中显示行数和列数均为n的随机整数矩阵。单击“排序”按钮Command2,奇数行从左到右升序排序,偶数行从左到右降序排序,结果显示在列表框List2中,输出时保持矩阵大小不变。运行结果如图所示。

请回答下列问题:

(1) 实现上述功能的 VB 程序如下,请在划线处填入合适的代码。

Dim a(1 To 100) As Integer

Dim n As Integer

Private Sub Command1_Click()

    Dim i As Integer, j As Integer

    Dim s As String, k As Integer

    n = Val(Text1.Text)

    For i = 1 To n

       

        For j = 1 To n

            k = (i - 1) * n + j

            a(k) = Int(Rnd() * 45 + 5) * 2

            s = s + " " + Str(a(k))

        Next j

        List1.AddItem s

    Next i

End Sub

Private Sub Command2_Click()

    Dim i As Integer, L As Integer, R As Integer

    Dim f As Integer

    For i = 1 To n

    If i Mod 2 = 1 Then f = 1 Else f = -1

       

        R = i * n

        List2.AddItem sort(L, R, f)

    Next i

End Sub

Function sort(left As Integer, right As Integer, order As Integer) As String

    Dim i As Integer, j As Integer, k As Integer

    Dim s As String, t As Integer

    s = ""

    For i = left To right - 1

        k = i

        For j = i + 1 To right

            If  Then k = j

        Next j

        If k <> i Then

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

        End If

        s = s + " " + Str(a(i))

    Next i

   

    sort = s

End Function

(2) 若删除上述VB程序中加框处代码,(选填:会/不会)影响程序运行结果。
【考点】
排序算法及程序实现;
【答案】

您现在未登录,无法查看试题答案与解析。 登录
综合题 困难
能力提升
真题演练
换一批