Next i End Sub
Private Sub convert(ch As String, s As String)
Dim m As Integer, k As Integer, n As Integer, i As Integer '**********SPACE********** s= 【?】 n = Asc(ch) Do While n > 0
'**********SPACE********** 【?】 s = m & s If m = 1 Then k = k + 1 End If n = n \\ 2 Loop
For i = 1 To 7 - Len(s) '将字符的二进制代码补足7位 s = \ Next i
If k Mod 2 = 0 Then '**********SPACE********** 【?】 Else
s = \ End If End Sub
第14题
'下面程序的功能是:找出仅由数字1、2、3、4组成的4位素数,要求每个素数由4个不 '同数字组成。算法提示:函数Validate用于验证一个4位数是否由4个不同数字组成。 '在函数中用A数组的各个元素分别对应数字0~9,只要某数字出现在四位数中,无论几次, '均将该数字对应的数组元素值置为1。程序界面参见下图。 Option Explicit
Private Sub Command1_Click() Dim i As Integer,Flg As Boolean For i=1234 To 4321 '**********SPACE********** 【?】
Call Prime(i,Flg) If Flg Then
If Validate(i) Then
Text1=Text1 & i & vbCrLf . End If End If Next i End Sub
Private Sub Prime(n As Integer,f As Boolean) Dim k As Integer
For k=2 To Sqr(n) '**********SPACE********** 【?】 Next k f=True End Sub
Private Function Validate(n As Integer) As Boolean Dim A(0 To 9)As Integer,s As String,i As Integer Dim s1 As String*1 '**********SPACE********** 【?】
For i=1 To Len(s) s1=Mid(s,i,1) '**********SPACE********** 【?】 Next i
If A(1)+A(2)+A(3)+A(4)=4 Then '**********SPACE********** 【?】 End If End Function
第15题
'本程序的功能是利用无穷级数求cos(x)的近似值,已知公式如图所示:
'当第n项的绝对值小于等于10^-7时计算终止。程序界面参见下图。 Option Explicit
Private Sub Command1_Click()
Dim X As Single, n As Integer, sum As Single Dim a As Single X = Text1
'**********SPACE********** 【?】 a = 1 n = 1 Do a = -a
'**********SPACE********** a = 【?】 sum = sum + a n = n + 1
'**********SPACE********** Loop Until 【?】 Text2 = sum End Sub
第16题
'定义方阵的一种范数为该方阵各列元素的绝对值之和中的最大值。以下程序的功能 '是求一个4×4方阵的范数。该方阵的数据是随机生成的-20~20之间的整数。 '程序界面参见下图。 Option Explicit Option Base 1
Private Sub Command1_Click() Dim a(4,4) As Integer
Dim i As Integer,j As Integer For i=1 T0 4 For j=1 To 4 '**********SPACE********** a(i,j)= 【?】
Picture1.Print Right(\ Next j
Picture1.Print Next i
'**********SPACE********** Text1.Text= 【?】 End Sub
Private Function fan(a()As Integer) As Integer Dim b(4) As Integer,max As Integer Dim i As Integer,j As Integer For i=1 To 4 For j=1 To 4 '**********SPACE********** b(j)=【?】 Next j Next i max=b(1) For i=2 To 4
If max
四、程序改错_共7题(选:3错*4’=12分) 第1题
'【题目】从10个各不相同的正整数中,剔除若干个'数。使得保留下来的数中, ' 任意两个数的和都不是平方数
'(所谓平方数就是该数等于某一个数的平方,例如'4就是一个平方数)。 Option Explicit Option Base 1
Private Sub Command1_Click()
Dim a(10) As Integer, i As Integer, j As Integer, p As Integer, k As Integer '**********FOUND********** Do While p < 10
k = Int(20 * Rnd) + 1 For j = 1 To p '**********FOUND**********
If k = a(j) Then Exit for Next j If j > p Then p = p + 1 a(p) = k
Text1 = Text1 & Str(k) End If Loop
Call delete(a, p) For i = 1 To p
Text2 = Text2 & Str(a(i)) Next i End Sub
Private Sub delete(a() As Integer, p As Integer) Dim idx As Integer, j As Integer, sum As Integer idx = 2
Do While idx < p
For j = 1 To idx - 1 sum = a(idx) + a(j)
If Int(Sqr(sum)) = Sqr(sum) Then Exit For Next j
'**********FOUND********** If j <= idx - 1 Then a(idx) = a(p) p = p - 1 Else
idx = idx + 1 End If Loop End Sub
第2题
'【题目】本程序的功能是生成一组(10个)两位的互质'数。
'一组互质数是指其中任意两个数之间都不存在除1以外'的任何公约数。
Private Sub Command1_Click()
Dim i As Integer, k As Integer, num(10) As Integer Dim flag As Boolean, n As Integer
num(1) = Int(Rnd * 90) + 10 k = 1 Do
'**********FOUND********** flag =flase
n = Int(Rnd * 90) + 10 For i = 1 To k
If gcd(num(i), n) <> 1 Then flag = False Next i If flag Then k = k + 1 num(k) = n End If
Loop Until k = 10 For i = 1 To 10
Text1 = Text1 & Str(num(i)) Next i End Sub
'**********FOUND**********
Private Function gcd(ByVal m%, ByVal n%) As Integer Dim r As Integer Do
r = m Mod n m = n n = r
Loop Until r = 0
'**********FOUND********** gcd = m End Function
第3题
'【题目】本程序的功能是:把10~20范围内的数分别表示成若干个质因子连乘的形式。 Option Explicit Option Base 1
Private Sub Command1_Click()
Dim j As Integer, pf() As Integer, i As Integer Dim st As String For i = 10 To 20 Call prime_f(i, pf) st = CStr(i) & \'**********FOUND********** For j = 1 To UBound(pf)-1 st = st & Str(pf(j)) & \ Next j
st = st & Str(pf(j)) List1.AddItem st Next i End Sub