第6章 过 程
Print \End Sub
Private Sub Form_Click()
x = 1 y = 1
Print \test
Print \End Sub
5)阅读程序,写出程序运行后的输出结果:_____。
Option Explicit
Private Sub Form_Click()
test 3 End Sub
Private Sub test(x As Integer)
Dim i As Integer If x <> 0 Then
Call test(x - 1)
For i = 1 To x Print x; Next i Print End If End Sub
6)阅读程序,写出程序运行后的输出结果:_____。
Option Explicit
Private Function f(a As Integer, b As Integer) As Integer
Dim i As Integer
Static m As Integer
i = 2: i = i + (m + 1): m = i + a + b f = m End Function
Private Sub form_click()
Dim k As Integer, m As Integer, p As Integer
k = 4: m = 1
- 26 -
((第6章 过 程
p = f(k, m): Print p; p = f(k, m): Print p End Sub
(7)阅读程序,写出程序运行后的输出结果:_____。
Option Explicit
Private Sub Form_Click()
Dim M As Integer, N As Integer
M = 1: N = 2
Print M + N + Fun1(M, N) M = 2: N = 1
Print Fun1(M, N) + Fun1(M, N) End Sub
Private Function Fun1(X As Integer, Y As Integer)
X = X + Y
Y = X + 3 Fun1 = X + Y End Function
(8)阅读程序,写出程序运行后的输出结果:_____。
Option Explicit
Private Sub Form_Click()
Dim A As Integer, B As Integer, K As Integer A = 2: B = 4
K = Fun((A), B) + Fun(A, B)
Print K End Sub
Private Function Fun(N As Integer, M As Integer) As Integer
Static K As Integer N = N + M K = K + N M = M + K Fun = N + M Print Fun; N; M End Function
(9)阅读程序,写出程序运行后的输出结果:_____。
Option Explicit
Dim A As Integer
- 27 -
第6章 过 程
Private Sub Form_Click()
Dim B As Integer, D As Integer A = 1: B = 2
D = fun(A, fun(A, B)) Print A, B, D End Sub
Private Function fun(K As Integer, N As Integer) As Integer
Print K, N K = N + A + K N = K + A + N fun = K + N Print fun End Function
(10)阅读程序,写出程序运行后的输出结果:_____。
Option Explicit
Private Function Sub2(x As Integer, y As Integer, z As Integer)
Sub2 = 2 * x + y + 3 * z End Function
Private Function Sub1(x As Integer, y As Integer, z As Integer) Sub1 = Sub2(z, x, y) + x End Function
Private Sub Form_Click()
Dim a As Integer, b As Integer, c As Integer a = 2: b = 3: c = 4 Print Sub1(c, b, a) End Sub
4、编程题:
(1)输入一个四位正整数,将其逆序输出(如输入为1975,输出为5791)。 (2)验证任意一个大于5的奇数可表示为3个素数之和。
(3)编写一个将N进制数转换为十进制数的通用程序。
(4)已知竞赛小组12名学生竞赛成绩,请排列出他们的名次(成绩由键盘输入,成绩相同的名次相同)。
(5)编写用于判断输入的正整数是否为降序数的函数。设正整数n=d1d2d3…dk,如果满足di≥di+1(i=1,2,…,k-1),则n就是一个降序数。如4321和9433都是降序数。
- 28 -
第6章 过 程
(6)求出2000以内的满足以下条件的正整数:该数本身不是素数,但它的所有因子之和是素数。提示:将素数的判断、因子之和的求解写成函数形式。
(7)编写一个求裴波拉契数列的递归过程,并将其前六项显示在文本框中。裴波拉契数列的通项公式如下:
?1,Fab(n)???Fab(n?2)?Fab(n?1),n?1,2n?3
(8)编写程序求下列数列的和,计算精确到第n项的值小于等于10-5为止。
y?12?12?4?12?4?6???12?4?6???2n??
(式中n=1, 2, 3, …)
(9)编写程序求下列级数的和,计算精确到第n项的值小于等于10-5为止。
S?x?x21?2?x32?3?x53?5???xfnfn?1??fn?? 0 < x < 1
其中:
?1,???1,?f?n?1?fn?2,n?1n?2 n?2fn(10)从键盘读取数组a和b的元素(各5个),a、b都是严格递增的(即元素从小到大排列,且无重复元素),将a、b合并成数组c,使c也严格递增。
- 29 -