2011年(春)上机 Visual Basic(VB02)答案
一、改错
【题目】本程序的功能是,找出使用公式m?m?41(m=1~100)生成的一个数据序列中的所有非素数,分别输出这些非素数的全部真因子。所谓数据n的真因子是指除1和n之外的因子。
Option Explicit
Private Sub Command1_Click() Dim m As Integer, p As Long Dim st1 As String
'st1 = \ 'err 位置错 For m = 1 To 100
p = m * m - m + 41 st1 = \
If Not prime(p) Then Call fac(p, st1)
List1.AddItem \第\项 \ End If Next m End Sub
Private Function prime(n As Long) As Boolean Dim k As Integer For k = 2 To Sqr(n)
If n Mod k = 0 Then Exit Function 'err For Next k
prime = True End Function
Private Sub fac(n As Long, st As String) Dim i As Integer For i = 2 To n \\ 2
If n Mod i = 0 Then st = st & i & \ End If Next i
st = Left(st, Len(st) - 1) 'err Right End Sub
二、编程题
【题目】编写程序,在一个数组中找出比第一个数组元素值大的数组元素中最小的元素值。如果第一个数组元素值本身为最大数,则输出“无”。
2Option Explicit Option Base 1
Private Sub CmdRun_Click()
Dim a(10) As Integer, i As Integer, minlarge As Integer Randomize For i = 1 To 10
a(i) = Int(Rnd * 90) + 10 Text1 = Text1 & Str(a(i)) Next i
Call search(a, minlarge) If minlarge = 0 Then Text2 = \无\ Else
Text2 = minlarge End If End Sub
Private Sub search(a() As Integer, minlarge As Integer)
Dim i As Integer, k As Integer, p As Integer, q As Integer For i = 2 To UBound(a) If a(i) > a(1) Then
minlarge = a(i): p = i Exit For End If Next i
For i = p + 1 To UBound(a)
If a(i) > a(1) And a(i) < minlarge Then minlarge = a(i): p = i End If Next i End Sub
Private Sub CmdClear_Click() Text1 = \ Text3 = \
CmdRun.SetFocus End Sub
Private Sub CmdExit_Click() End End Sub