VB复习题(带答案)(1) 下载本文

内容发布更新时间 : 2024/12/23 12:58:17星期一 下面是文章的全部内容请认真阅读。

程序填空

1、'功能:要求输入若干学生的成绩,计算平均分和高于平均分的人数,并将这两个数据放入数 x1 = p

x2 = p

组的最后。

Private Sub Form_Click()

Dim mark() As Integer, i%, n%, aver n = InputBox(\输入学生的人数\ ReDim mark(1 To n) aver = 0

For i = 1 To n

mark(i) = Int(Rnd * 101) aver = aver + mark(i) Next i

'**********SPACE********** preserve ReDim 【?】 mark(1 To n + 2) '增加两个元素,存放平均分和高于平均分的人数,原来的学生成绩仍保留 mark(n + 1) = aver / n mark(n + 2) = 0 For i = 1 To n

'**********SPACE**********

If mark(i) > mark(【?】) Then mark(n + 2) = mark(n + 2) + 1 n+1 Next i

For i = 1 To n

Print \ Next i

Print \平均分=\高于平均分人数=\End Sub

2、功能:以下程序的功能如(图1)。 Private Sub Form_Click()

Dim a%, b%, c%, d!, x1, x2, p, q, r a = InputBox(\请输入a的值\b = InputBox(\请输入b的值\c = InputBox(\请输入c的值\d = b * b - 4 * a * c p = -b / (2 * a) If d >= 0 Then

'**********SPACE********** If 【?】 Then d>0 r = Sqr(d) / (2 * a) x1 = p + r x2 = p - r Else '**********SPACE********** 【?】 end if Print \'**********SPACE********** 【?】 else q = Sqr(-d) / (2 * a)

Print \End If End Sub

3、功能:以下程序用于判断一个正整数(≥3)是否为素数。 Private Sub Form_Click()

n = InputBox(\请输入一个正整数(≥3)\k = Int(Sqr(n)) i = 2 swit = 0

'**********SPACE**********

Do While i <= k And 【?】 swit=0 '**********SPACE********** If 【?】 Then n mod i=0 swit = 1 Else

'**********SPACE********** 【?】 i=i+1 End If Loop

If swit = 0 Then

Print n; \是一个素数\Else

Print n; \不是素数\End If End Sub

4、功能:下面的程序段,用于实现在一个nXm的矩阵中,找出值最大的元素所在的行和列,并输出其值及行号和列号。 Private Sub Form_Click() Dim mat() As Integer

Dim n as integer, m As Integer

n = Val(InputBox(\请输入矩阵的行数\m = Val(InputBox(\请输入矩阵的列数\

1

'**********SPACE********** 【?】 ReDim mat(n,m) As Integer For i = 1 To n For j = 1 To m

mat(i, j) = InputBox(\请输入数组元素值\ mat(i, j) = Val(mat(i, j)) Next j Next i

Print \所建立的矩阵为\For i = 1 To n For j = 1 To m Print mat(i, j); Next j Print Next i

Max = mat(1, 1) For i = 1 To n For j = 1 To m

'**********SPACE**********

If 【?】 Then mat(I,j)>max Max = mat(i, j) col = j

'**********SPACE********** 【?】 row=i End If Next j Next i Print

Print \矩阵最大的元素的值为:\Print \它所在的行号为:\列号为:\End Sub

5、功能:以下程序段用于实现矩阵转置,即将一个n×m的矩阵的行和列互换 Private Sub Form_Click() Const n = 3 Const m = 4

Dim a(n, m), b(m, m) As Integer For I = 1 To n For j = 1 To m

a(I, j) = Int(Rnd * 90) + 10 Next j

'**********SPACE********** 【?】 next i For I = 1 To n For j = 1 To m

'**********SPACE********** 【?】 b(j,i)=a(I,j) Next j Next I

Print \矩阵转置前\For I = 1 To n For j = 1 To m Print a(I, j); Next j

'**********SPACE********** 【?】 print Next I

Print \矩阵转置后\For I = 1 To m For j = 1 To n Print b(I, j); Next j Print Next I End Sub

6、下面过程max()用于求3个数中最大值,利用这个过程求5个数中最大值。Private Sub Form_Click()

Print \个数34、124、68、73、352的最大值是:\max1 = max(34, 124, 68)

'**********SPACE********** max1 =【?】 max(max1,73,352) Print max1 End Sub

Public Function max(ByVal a%, ByVal b%, ByVal c%) '**********SPACE********** If 【?】 Then a>b m = a Else

m = b End If

'**********SPACE********** 2

If 【?】 Then m>c max = m Else

max = c End If

End Function

7、功能:以下程序段利用随机函数生成15个10-100之间的整数,然后用选择法将其从小到大排Dim i As Integer, j As Integer, x As Integer

Randomize For i = 1 To n

a(i) = Int(90 * Rnd) + 10 Next i

For i = 1 To n Print a(i); 序。

Private Sub Form_Click() Const n = 15

Dim a(1 To n) As Integer

Dim i As Integer, j As Integer, t As Integer, min As Integer Randomize For i = 1 To n

'**********SPACE********** a(i) =【?】 int(rnd*91+10) Next i

For i = 1 To n Print a(i); Next i Print

For i = 1 To n - 1

'**********SPACE********** 【?】 t=I For j = i + 1 To n

If a(j) < a(t) Then t = j Next j

'**********SPACE********** If 【?】 Then t<>i min = a(i): a(i) = a(t): a(t) = min End If Next i

For i = 1 To n Print a(i); Next i End Sub

8、功能:下面的程序段用于实现以下功能:利用冒泡法将一组整数从小到大排序。Private Sub Form_Click() Const n = 15

Dim a(1 To n) As Integer, work As Boolean Next i Print

'**********SPACE********** For i = n To 2 【?】 step -1 work = True For j = 1 To i - 1

If a(j) > a(j + 1) Then

x = a(j): a(j) = a(j + 1): a(j + 1) = x '**********SPACE********** 【?】 work=false End If Next j

'**********SPACE********** If work Then 【?】 exit for Next i

For i = 1 To n Print a(i); Next i End Sub

9、功能:以下程序的功能如(图1)。 Private Sub Form_Click()

Dim s As Single, m As Integer, p# s = 1

For m = 1 To 10

'**********SPACE********** p=【?】n(m) s = s + 1 / p Next m Print s End Sub

Function n(k%) p = 1

3