VB考试必备十道编程题VB考试必备的十道编程题
- 格式:docx
- 大小:13.42 KB
- 文档页数:4
VB考试必备十道编程题VB考试必备的十道编程题
(1)计算 sum=1/1!+1/2!+1/3!+.......+1/10!
Private Sub Command1_Click()
Dim m As Integer, n As Long, s As Double
n = 1
s = 0
For m = 1 To 10
n = m * n
s = s + 1 / n
Next
Print s
End Sub
(2)求前n项和 s=1/!-1/2!+1/3!-1/4!+.........+(-1)^(n-1)*1/n!+.......当求和过程某项绝对值小于0.0001时结束(该项参与求和),输出s,保留6位小数。Private Sub Command1_Click()
Dim m As Double, n As Long, s As Double, p As Integer
s = 0: n = -1: m = 1: p = 1
Do While 1 / m >= 0.0001
m = m * p
p = p + 1
n = -n
s = s + n / m
Loop
s = Format(s, "0.000000")
Print s
End Sub
(3)裴波那契数列前两项是1、1,以后的每一项是其相邻前两项之和。求数列前三十项的和 FS
Private Sub Command1_Click()
Dim a&, b&, c&, i&, FS&
a = 1:
b = 1: FS = 2
For i = 1 To 28
c = a + b
a = b
b = c
FS = FS + c
Next
MsgBox (FS)
End Sub
(4)随机输出一个小写字母
Private Sub Command1_Click()
Dim a As String
a = Chr(Int(97 + 26 * Rnd))
print (a)
End Sub
随机输出一个大写字母
Private Sub Command1_Click()
Dim a As String
a = Chr(Int(65+26*rnd))
Print (a)
End Sub
(5)定义一个下标上界为10的整形数组,并用随机函数为其赋值(值处于[0,100]
之间),计算数组中所有偶数的和
Private Sub Command1_Click()
Dim a%(10)
Dim i%, s%
For i = 0 To 10
a(i) = 101 * Rnd()
Next
For i = 0 To 10
If a(i) Mod 2 = 0 Then
s = s + a(i)
End If
Next
Print "所有偶数和为:"; s
End Sub
(6)如果一个三位数等于其各位数字的立方和,择成其为水仙花数。输入一个三位数,判断是否属于水仙花数。
Private Sub Command1_Click()
Dim a%, i%, j%, k%
a = InputBox("请输入一个三位数!")
i = a \ 100
j = a \ 10 Mod 10
k = a Mod 10
If a = i ^ 3 + j ^ 3 + k ^ 3 Then
MsgBox ("Yes")
Else
MsgBox ("No")
End If
End Sub
(7)用inputbox输入一个年份是否是闰年,若是输出“Yes”若不是输出“No”Private Sub Command1_Click()
Dim a As Integer
a = InputBox("请输入年份")
If a Mod 400 = 0 Or a Mod 4 = 0 And a Mod 100 <> 0 Then
MsgBox ("Yes")
Else
MsgBox ("No")
End If
End Sub
(8)输入三个数a,b,c判断是否能够成三角形,若能计算其面积并用msgbox 输出;若不能用msgbox输出“不能构成三角形”
Private Sub Command1_Click()
Dim a!, b!, c!
a = InputBox("a=")
b = InputBox("b=")
c = InputBox("c=")
If a + b > c And b + c > a And a + c > b Then
p = (a + b + c) / 2
s = Sqr(p * (p - a) * (p - b) * (p - c))
MsgBox (s)
Else
MsgBox ("不能构成三角形")
End If
End Sub
(9)输入三个数a,b,c,编写程序使得a<=b<=c。并输出这三个数。
Private Sub Command1_Click()