VB考试复习题 下载本文

Next i Print

Print \End Sub

26、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[20,50]内的整数,输出每行中的最大值和下标。

Dim a%(3, 3), i%, j%, imax% Private Sub Command1_Click() For i = 0 To 3 For j = 0 To 3

a(i, j) = Int(Rnd * 31 + 20) Picture1.Print a(i, j); \ Next j

Picture1.Print Next i imax = 0

For i = 0 To 3 For j = 0 To 3

If a(i, imax) < a(i, j) Then imax = j End If Next j

Picture2.Print \第\行\最大值为\下标为\ Next i End Sub

27、声明一个整型的二维数组a(1 to 4,1 to 4),用随机函数产生各元素,范围介于[1,20]之间,编程将第1行和第3行对应元素交换。 Private Sub Form_Click() Dim a%(1 To 4, 1 To 4), i%, j% Print \排序前\For i = 1 To 4 For j = 1 To 4

a(i, j) = Int(Rnd * 20 + 1) Print Tab(j * 4); a(i, j); Next j Print Next i

Print \排序后\For j = 1 To 4

t = a(1, j): a(1, j) = a(3, j): a(3, j) = t Next j

For i = 1 To 4

For j = 1 To 4

Print Tab(j * 4); a(i, j); Next j Print Next i End Sub

28.编写一个过程计算并输出区间[100,1000]内所有素数的和(要求判断素数使用过程(函数或子过程)来实现)。调用该过程。(提示:只能被1 和自身整除的自然数成为素数) Function sushu%(ByVal x%) Dim m%, Tag As Boolean, i% m = Val(x) Tag = True

For i = 2 To m - 1

If (m Mod i) = 0 Then Tag = False Next i

If Tag Then sushu = x End Function

Private Sub Command1_Click() Dim i%, k%

For i = 100 To 1000

If sushu(i) Then List1.AddItem i Next i Sum = 0

For k = 0 To List1.ListCount - 1 Sum = Sum + Val(List1.List(k)) Next k

Label1 = \[100,1000]内所有素数和为:\End Sub

29,30、随机生成一个整型的二维数组,范围在[10,20]之间,以下三角形式输出该数组。 Private Sub Form_Click() Dim a(4, 4) As Integer For i = 0 To 4 For j = 0 To 4

a(i, j) = Int(Rnd * 11 + 10) Next j Next i

For i = 0 To 4 For j = 0 Toi Print a(i, j); Next j Print

Next i End Sub

以上三角形式输出该数组 Dim a%(4, 4)

Private Sub Command1_Click() Picture1.Cls For i = 0 To 4 For j = i To 4

Picture1.Print Tab(j * 6); a(i, j); Next j

Picture1.Print Next i End Sub

Private Sub Form_Load() For i = 0 To 4 For j = 0 To 4

a(i, j) = Int(Rnd * 11 + 10) Next j Next i End Sub

31、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[40,80]内的整数,求它的最大值及所对应的下标。 Private Sub Form_Click() Dim a(3, 3), max%, imax% For i = 0 To 3 For j = 0 To 3

a(i, j) = Int(Rnd * 41 + 40) Next j Next i

For i = 0 To 3 For j = 0 To 3 Print a(i, j); Next j Print Next i

For i = 0 To 3 For j = 0 To 3

If max

Print \End Sub

32.已知一维数组a中元素已排序,分别为{12,15,21,25,27,35,36,39,48,52},编程将30插入数组,使a仍保持有序。 Private Sub Form_load() Dim a(), i%, k%, x%, n%

a = Array(1, 4, 7, 9, 12, 14, 23, 56) n = UBound(a) For i = 0 To n

List1.AddItem a(i) Next i End Sub

Private Sub command1_Click() x = Val(Text1)

For k = 0 To List1.ListCount - 1 If x

List1.AddItem x, k End Sub

(做的时候一个命令控件、一个text文本框和一个list)

33、已知数组a(),编程删除a中第5个元素。数组a中的元素分别为{12,6,4,89,75,63,100,20,31}。 Private Sub Form_Click() Dim a(), i%, n%

a = Array(12, 6, 4, 89, 75, 63, 100, 20, 31) n = UBound(a) For i = 0 To n Print a(i); Next i Print

For i = 5 To n a(i - 1) = a(i) Next i n = n - 1

ReDim Preserve a(n) For i = 0 To n Print a(i); Next i Print End Sub

34、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[40,80]内的整数,求它的最小值及所对应的下标。

Dim a%(3, 3), i%, j%, imin% Private Sub Command1_Click() For i = 0 To 3 For j = 0 To 3

a(i, j) = Int(Rnd * 21 + 40) Picture1.Print a(i, j); \ Next j

Picture1.Print Next i imax = 0

For i = 0 To 3 For j = 0 To 3

If a(i, imin) > a(i, j) Then imin = j End If Next j

Picture2.Print \第\行\最小值为\下标为\ Next i End Sub

1、找出被3、5、7除,余数均为1的最小的5个正整数。 Private Sub Command1_Click() Dim countN%, n% countN = 0 n = 1 Do

n = n + 1

If n Mod 3 = 1 And n Mod 5 = 1 And n Mod 7 = 1 Then Print n

countN = countN + 1 End If

Loop Until countN = 5 End Sub

2、编程显示100~500之间所有的水仙花数之和。(水仙花数是3位数,其各位数之和等于该