江苏省高等院校计算机等级考试vb书后习题答案 .pdf
第 6 章 数组习题参考答案6-1 请同学上机运行代码。6-2 Option Explicit Option Base 1 Private Sub Command1_Click() Dim i As Integer, t As Integer Dim a(15) As Integer Randomize For i = 1 To 15 a(i) = Int(100 * Rnd) Text1 = Text1 & Str(a(i) Next i For i = 1 To 7 t = a(i) a(i) = a(16 - i) a(16 - i) = t Next i For i = 1 To 15 Text2 = Text2 & Str(a(i) Next i End Sub Private Sub Command2_Click() Text1 = Text2 = End Sub Private Sub Command3_Click() End End Sub 6-3 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 17 页 - - - - - - - - - Option Explicit Dim a(20) As Integer Private Sub Command1_Click() Dim i As Integer Randomize For i = 1 To 20 a(i) = Int(90 * Rnd) + 10 Text1 = Text1 & Str(a(i) Next i End Sub Private Sub Command2_Click() Dim i As Integer, j As Integer, f As Boolean Dim n As Integer For i = 1 To 20 f = True For j = i + 1 To 20 If a(i) = a(j) Then f = False Next j If f = True Then n = n + 1 Next i Text2 = n End Sub Private Sub Command3_Click() Text1 = End Sub Private Sub Command4_Click() End End Sub 6-4 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 17 页 - - - - - - - - - Option Explicit Dim a(20) As Integer Private Sub Command1_Click() Dim i As Integer Randomize For i = 1 To 20 a(i) = Int(90 * Rnd) + 10 Text1 = Text1 & Str(a(i) Next i End Sub Private Sub Command2_Click() Dim i As Integer, j As Integer, max As Integer Dim n As Integer, w As Integer, p As Integer For i = 1 To 20 n = 0: p = i For j = 1 To 4 If i 20 Then p = p - 20 n = n + a(p) Next j If n max Then max = n w = i End If Next i Text2 = max Picture1.Print w & - & w + 4 End Sub Private Sub Command3_Click() Text1 = End Sub Private Sub Command4_Click() End End Sub 6-5 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 17 页 - - - - - - - - - Dim a(5, 5) As Integer Private Sub Command1_Click() For i = 1 To 4 For j = 1 To 5 a(i, j) = Int(9 * Rnd) + 1 Picture1.Print a(i, j); Next j Picture1.Print Next i End Sub Private Sub Command2_Click() For j = 1 To 5 s = 0 For i = 1 To 4 s = s + a(i, j) Next i Text1 = Text1 & Str(s) Next j End Sub Private Sub Command3_Click() For i = 1 To 4 s = 0 For j = 1 To 5 s = s + a(i, j) Next j Text2 = Text2 & Str(s) & Chr(13) & Chr(10) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 17 页 - - - - - - - - - Next i End Sub Private Sub Command4_Click() Text1 = Text2 = Picture1.Cls End Sub Private Sub Command5_Click() End End Sub 6-6 Option Explicit Private Sub Command1_Click() Dim a(5, 5), i As Integer, j As Integer Dim n As Integer, m As Integer, t As Integer Randomize For i = 1 To 5 For j = 1 To 5 - 1 a(i, j) = Int(40 * Rnd) * 2 + 10 Next j Next i For i = 1 To 5 For j = 5 To 6 - i Step -1 a(i, j) = Int(40 * Rnd) * 2 + 11 Next j Next i For i = 1 To 5 For j = 1 To 5 Text1 = Text1 & Str(a(i, j) Next j Text1 = Text1 & vbCrLf 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 17 页 - - - - - - - - - Next i End Sub 6-7 同 6-4 6-8 Dim a(5, 5) As Integer Private Sub Command1_Click() Randomize For i = 1 To 5 For j = 1 To 5 a(i, j) = Int(100 * Rnd) + 1 Picture1.Print Right( & a(i, j), 4); Next j Picture1.Print Next i End Sub Private Sub Command2_Click() For i = 1 To 5 For j = 1 To 5 s = s + a(i, j) Next j Next i Text1 = Str(s) End Sub Private Sub Command3_Click() For i = 1 To 5 For j = 1 To 5 If i = 1 Or j = 1 Or i = 5 Or j = 5 Then 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 17 页 - - - - - - - - - s = s + a(i, j) End If Next j Next i Text2 = Str(s) End Sub Private Sub Command4_Click() For i = 1 To 5 For j = 1 To 5 If i = j Or j = 6 - i Then s = s + a(i, j) End If Next j Next i Text3 = Str(s) End Sub 6-9 Option Explicit Private Sub Command1_Click() Dim a() As Integer, i As Integer, j As Integer Dim max As Integer, k As Integer, f As Boolean Dim n As Integer, m As Integer, t As Integer n = InputBox( 输入矩阵的行数:) m = InputBox( 输入矩阵的列数:) ReDim a(n, m) Randomize For i = 1 To n For j = 1 To m a(i, j) = Int(90 * Rnd) + 10 Text1 = Text1 & Str(a(i, j) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 17 页 - - - - - - - - - Next j Text1 = Text1 & vbCrLf Next i For i = 1 To n max = 0 For j = 1 To m If a(i, j) max Then max = a(i, j) t = j End If Next j For k = 1 To n If a(k, t) n Then Picture1.Print 鞍点: ; a(i, t); 第; i; 行; t; 列 f = True End If Next i If f = False Then Picture1.Print 没有鞍点 End Sub Private Sub Command2_Click() Text1 = Picture1.Cls End Sub Private Sub Command3_Click() End End Sub 6-10 Option Explicit 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 17 页 - - - - - - - - - Private Sub Form_Click() Dim a() As Integer, n As Integer Dim i As Integer, j As Integer n = InputBox( 层数 ) ReDim a(n, n) For i = 1 To n Print Tab(20 - 2 * i); For j = 1 To i If i = 1 Or i = j Then a(i, j) = 1 Else a(i, j) = a(i - 1, j - 1) + a(i - 1, j) End If Print Left(a(i, j) & , 4); Next j Print Next i End Sub 6-11 Option Explicit Private Sub Command1_Click() Dim a(4, 5), i As Integer, j As Integer Dim b(4) As Integer, max As Integer, min As Integer Dim n As Integer, m As Integer, t As Integer Randomize max = 0: min = 32767 For i = 1 To 4 For j = 1 To 5 a(i, j) = Int(90 * Rnd) + 10 Text1 = Text1 & Str(a(i, j) b(i) = b(i) + a(i, j) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 17 页 - - - - - - - - - Next j Text1 = Text1 & vbCrLf If max b(i) Then min = b(i): n = i End If Next i Label1.Caption = max 行和: & max & min行和: & min & vbCrLf & max行: & m & min 行: & n & 交换这两行 For j = 1 To 5 t = a(m, j) a(m, j) = a(n, j) a(n, j) = t Next j For i = 1 To 4 For j = 1 To 5 Text2 = Text2 & Str(a(i, j) Next j Text2 = Text2 & vbCrLf Next i End Sub 6-12 Option Explicit Private Sub Command1_Click() Dim a(), i As Integer, j As Integer Dim b(4) As Integer, max As Integer, min As Integer Dim n As Integer, m As Integer, t As Integer Randomize n = InputBox( 矩阵阶数 n:) ReDim a(n, n) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 17 页 - - - - - - - - - For i = 1 To n For j = 1 To n a(i, j) = Int(90 * Rnd) + 10 Text1 = Text1 & Str(a(i, j) Next j Text1 = Text1 & vbCrLf Next i For i = 1 To n For j = 1 To n Text2 = Text2 & Str(a(j, i) Next j Text2 = Text2 & vbCrLf Next i End Sub Private Sub Command2_Click() Text1 = Text2 = End Sub Private Sub Command3_Click() End End Sub 6-13 Option Explicit Option Base 1 Private Sub Command1_Click() Dim a(3, 3) As Integer, i As Integer, j As Integer Dim max As Long, k As Long, f As Integer Dim n As Integer, m As Integer Randomize For i = 1 To 3 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 17 页 - - - - - - - - - For j = 1 To 3 a(i, j) = Int(90 * Rnd) + 10 Text1 = Text1 & Str(a(i, j) Next j Text1 = Text1 & vbCrLf Next i For i = 1 To 3 主对角线方向m = i k = 1 For j = 1 To 3 k = k * a(m, j) m = m + 1 If m 3 Then m = 1 Next j If k max Then max = k n = i f = 1 End If Next i For i = 1 To 3 辅对角线方向m = i k = 1 For j = 1 To 3 k = k * a(j, m) m = m - 1 If m max Then max = k n = i f = 2 End If Next i Picture1.Print 最大组元素: If f = 1 Then For i = 1 To 2 Picture1.Print a(; n; ,; i; )=; a(n, i); ; n = n + 1 If n 3 Then n = 1 Next i Picture1.Print a(; n; ,; i; )=; a(n, i) ElseIf f = 2 Then For i = 1 To 2 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 17 页 - - - - - - - - - Picture1.Print a(; i; ,; n; )=; a(i, n); ; n = n - 1 If n 1 Then n = 3 Next i Picture1.Print a(; i; ,; n; )=; a(i, n) End If Picture1.Print 乘积 =; max End Sub Private Sub Command2_Click() Text1 = Picture1.Cls End Sub Private Sub Command3_Click() End End Sub 6-13(2) Option Base 1 Option Explicit Private Sub Form_Click() Dim a(3, 3) As Integer, i As Integer, m As Single Dim j As Integer, k As Integer, max As Single Dim ci As Integer, cj As Integer, ck As Integer Print 生成的二维数组: Randomize For i = 1 To 3 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 17 页 - - - - - - - - - For j = 1 To 3 a(i, j) = Int(Rnd * 9) + 1 Print a(i, j); Next j Print Next i Print 不同行不同列的三个数的乘积是: max = a(1, 1) * a(2, 2) * a(3, 3) ci = 1: cj = 2: ck = 3 Randomize For i = 1 To 3 For j = 1 To 3 For k = 1 To 3 If i j And j k And i k Then m = a(1, i) * a(2, j) * a(3, k) Print m; If m max Then max = m ci = i cj = j ck = k End If End If Next k Next j Next i Print Print 最大值为: & Str(max) Print A(1, & CStr(ci) & )= & Str(a(1, ci) Print A(2, & CStr(cj) & )= & Str(a(2, cj) Print A(3, & CStr(ck) & )= & Str(a(3, ck) End Sub 6-14 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 17 页 - - - - - - - - - Option Explicit Private Sub Command1_Click() Dim a(3, 3) As Integer, i As Integer, j As Integer Dim k As Integer i = 3: j = 2: a(i, j) = 1: k = 1 Do Until k 9 k = k + 1 If i + 1 3 And j + 1 = 3 Then i = 1: j = j + 1 ElseIf i + 1 3 Then i = i + 1: j = 1 ElseIf i + 1 3 And j + 1 3 Then i = i - 1 ElseIf i + 1 = 3 And j + 1 = 3 And a(i + 1, j + 1) 0 Then i = i - 1 Else i = i + 1: j = j + 1 End If a(i, j) = k Loop For i = 1 To 3 For j = 1 To 3 Picture1.Print a(i, j); Next j Picture1.Print Next i End Sub 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 17 页 - - - - - - - - - Private Sub Command2_Click() Dim a() As Integer, i As Integer, j As Integer Dim k As Integer Dim n As Integer n = InputBox( 幻方阶数: ) ReDim a(n, n) i = n: j = (n + 1) / 2: a(i, j) = 1: k = 1 Do Until k n * n k = k + 1 If i + 1 n And j + 1 = n Then i = 1: j = j + 1 ElseIf i + 1 n Then i = i + 1: j = 1 ElseIf i + 1 n And j + 1 n Then i = i - 1 ElseIf i + 1 = n And j + 1 = n And a(i + 1, j + 1) 0 Then i = i - 1 Else i = i + 1: j = j + 1 End If a(i, j) = k Loop For i = 1 To n For j = 1 To n Picture1.Print Right( & a(i, j), 4); Next j Picture1.Print Next i End Sub 6-15 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 17 页 - - - - - - - - - Option Explicit Option Base 1 Private Sub Command1_Click() Dim a() As Integer, i As Integer, j As Integer Dim max As Long, k As Long, f As Integer Dim n As Integer, m As Integer Randomize f = InputBox( 输入方阵阶数:) ReDim a(f, f) For i = 1 To f For j = 1 To f a(i, j) = Int(90 * Rnd) + 10 Text1 = Text1 & Str(a(i, j) Next j Text1 = Text1 & vbCrLf Next i For i = 1 To f 副对角线方向m = i k = 0 For j = 1 To f k = k + a(j, m) m = m - 1 If m max Then max = k n = i End If Next i Picture1.Print max :; max For i = 1 To f Picture1.Print a(; i; ,; n; )=; a(i, n) n = n - 1 If n 1 Then n = f Next i End Sub Private Sub Command2_Click() Text1 = Picture1.Cls End Sub Private Sub Command3_Click() End End Sub 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 17 页 - - - - - - - - -