您好,1.设有如下两组数据: (1)1,3,5,2,4,18,50,25 (2)5,27,30,35,60,41,87,33 编写一个程序,把上面两组数据分别读入两个数组中,然后把两个数组中对应下标的元素相加,即1+5,3+27,……,25+33,并把相应的结果放入第三个数组中,最后输出第三个数组的值 代码 Private Sub Command1_Click() Dim i Dim x() Dim y() Dim z(0 To 7) x = Array(1, 3, 5, 2, 4, 18, 50, 25) y = Array(5, 27, 30, 35, 60, 41, 87, 33)
For i = 0 To 7 z(i) = x(i) + y(i) Print z(i); Next i
End Sub 运行界面
2.有一个n×m的矩阵,编写程序,找出其中最大的那个元素所在的行和列,并输出其值及行号和列号。 代码 Private Sub Command1_Click() Dim a(1 To 10, 1 To 10) As Integer Dim m%, n%, x% For i = 1 To 10 For j = 1 To 10 a(i, j) = Int(Rnd * 101) Print Tab((j - 1) * 5 + 2); a(i, j); Next j Next i Print x = a(1, 1) For i = 1 To 10 For j = 1 To 10 If a(i, j) > x Then m = i: n = j: x = a(i, j) Next j Next i Print "最大数为:" & x & ",它为第 " & m & "行," & "第" & n & "列的元素。" End Sub 运行界面
代码 Option Base 1 Private Sub Command1_Click() Dim a%(1 To 10) Dim i% For i = 1 To 10 a(i) = InputBox("请输入第" & i & "个数") Print a(i); Next i Print For i = 1 To 5 t = a(i) a(i) = a(10 - i + 1) a(10 - i + 1) = t Print a(i); Next i For i = 1 To 5 Print a(i + 5); Next i End Sub 运行界面
4.输入若干个学生的一门课的成绩,统计各分数段的人数。按小于60分、60~69分、70~79分、80~89分、90~100分各为一个分数段。 代码 Private Sub Command1_Click() Dim a$(), k%, y Dim x(0 To 10) As Integer a = Split(Text1.Text, ",") For Each y In a k = y \ 10 x(k) = x(k) + 1 Next y Picture1.Print "统计结果如下" Picture1.Print "90-100分有:" & x(9) + x(10) & "人" For i = 8 To 6 Step -1 Picture1.Print i * 10 & "分—"; i * 10 + 9 & "分有" & x(i) & "人" Next i For i = 1 To 5 z = z + x(i) Next i Picture1.Print "60分以下的有:" & z & "人" End Sub 运行界面
5.我国身份证号码的第18位是由前17位通过公式计算出来的,请编程:要求从键盘上输入其一身份证号码的前17位,求出第18位 代码 Private Sub Command1_Click() Dim W(), A(0 To 16), s%, y% For i = 0 To 16 W = Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2) A(i) = InputBox("请输入第" & i + 1 & "个数") Print A(i); s = s + A(i) * W(i) Next i Print y = s Mod 11 y = y + 1 m = Choose(y, "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2")
Print "第十八位是:" & m End Sub 运行界面
6.有一个数列,前两项为1,从第三项开始,每一项都是前现两项之和,输出该数列前30项。 代码 Option Base 1 Private Sub Command1_Click() Dim a!(1 To 30) a(1) = 1: a(2) = 2 Print a(1); a(2) For i = 3 To 30 a(i) = a(i - 1) + a(i - 2) Print a(i); Next i End Sub 运行界面
7.随机产生15个不重复的A~Z之间的大写字母,存放在字符数组中。
代码 Private Sub Command1_Click() Dim a(1 To 15) For i = 1 To 15 a(i) = Int(Rnd * 26) + 65 Next i For i = 1 To 14 For j = i + 1 To 15 If a(j) = a(i) Then a(j) = Int(Rnd * 26) + 65 Next j Print Chr(a(i)); Next i End Sub 运行界面
8.将从键盘上输入的一个数插入到数组指定位置。 代码 Private Sub Command1_Click() Dim a(), l%, u% Dim x%, k% a = Array(1, 2, 3, 4, 5, 6, 7, 9, 10) l = LBound(a) u = UBound(a) For i = l To u Print a(i); Next i Print k = InputBox("请输入要插入的位置") x = InputBox("请输入要插入的数") ReDim Preserve a(l To u + 1) For i = u To k Step -1 k = i a(i + 1) = a(i) Next i a(k) = x For i = l To u + 1 Print a(i); Next i End Sub 运行界面
9.在数组中查找从键盘上输入的一个数,如果找不到输出相应信息,如果找到,则删除该元素。 代码 Private Sub Command1_Click() Dim a(), p As Boolean, x%, l%, u%, i%, k% a = Array(1, 5, 8, 9, 4, 6, 3, 7) l = LBound(a): u = UBound(a) For i = l To u Print a(i); Next i Print x = Val(InputBox("请输入要删除的数")) For i = l To u If x = a(i) Then p = True: k = i Exit For End If Next i If p = True Then For i = k + 1 To u a(i - 1) = a(i) Next i u = u - 1 ReDim Preserve a(l To u) For i = l To u Print a(i); Next i Else MsgBox "未找到!" End If End Sub 运行界面
10.求一个M行N列的矩阵四周元素之和,元素值可以随机产生,M、N从键盘输入。 代码 Private Sub Command1_Click() Dim a%(), m%, n%, i%, j%, b%, c%, d%, e%, f% m = InputBox("请输入该矩阵的行数") n = InputBox("请输入该矩阵的列数") ReDim a%(1 To m, 1 To n) For i = 1 To m For j = 1 To n a(i, j) = Int(Rnd * 11) Print Tab((j - 1) * 6 + 3); a(i, j); Next j Next i For j = 1 To n b = b + a(1, j) e = e + a(m, j) Next j For i = 2 To m - 1 c = c + a(i, 1) d = d + a(i, n) Next i f = b + c + d + e Print Print f