20道 vb习题答案
二、编程题
1. 输入三角形的三条边a,b,c的值,根据其数值,判断能否构成三角形。若能则求出三角形的面积。
Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer
Dim s As Single, area As Single
a = InputBox("请输入边长a")
b = InputBox("请输入边长b")
c = InputBox("请输入边长c")
If a + b > c And a + c > b And b + c > a Then
Print "能构成三角形"
s = (a + b + c) / 2
area = Sqr(s * (s–a) * (s–b) * (s-c )
Print "面积为:"; area
Else
Print "不能构成三角形"
End If
End Sub
2. 编一程序:根据上网时间计算上网费用。计算的方法如下:为了鼓励多上网,每月收费最多不超过100元。
30元基数 <10 h
费用=
每小时2元 10~60 h
每小时1.5元 >60 h
Private Sub Command1_Click()
Dim a As Integer, page As Single
a = Val(InputBox("请输入上网的时长"))
If a > 0 Then
Select Case a
Case Is < 10
page = 30
Case Is < 60
page = a * 2
Case Else
page = a * 1.5
End Select
Else
MsgBox "没有上网,无需交费"
End If
If page > 100 Then page = 100
Print "您本月共上网"; a; "小时,需交费:"; page; "元"
End Sub
3. 有一个长阶梯,如果每步跨2阶最后剩1阶,如果每步跨3阶最后剩2阶,如果每步跨4阶最后剩3阶,如果每步跨5阶最后剩4阶,如果每步跨6阶最后剩5阶,只有当如果每步跨7阶时恰好走完,问这个阶梯至少有多少阶?
Private Sub Command1_Click()
Dim a As Integer, i As Integer
Do While True
i = i + 1
vb程序设计教程第二版答案If i Mod 2 = 1 And i Mod 3 = 2 And i Mod 4 = 3 And i Mod 5 = 4 And i Mod 6 = 5 _And i Mod 7 = 0 Then
Print "该阶梯至少有"; i; "阶"
Exit Do
End If
Loop
End Sub
4. 一个球从100 m高度自由下落,每次落地后反跳回原高度的一半,再落下。求它在第10次落地时,共经过多少m?最后一次反弹高度多少?
Private Sub Command1_Click()
Dim i As Integer, s As Single, h As Single
h = 10
For i = 2 To 10
s = s + h
h = h / 2
s = s + h
Next i
Print "共经过"; s; "米。最后一次反弹高度为"; h
End Sub
5. 有一根长度为321m的钢材料,要将它截取成两种规格的短料,规格a、b的长度分别为17 m和27 m,问分隔成a、b各多少段后,剩余的残料r最少?
Private Sub Command1_Click()
Dim i As Integer, r As Integer, a As Integer, b As Integer,min As Integer
min = 17
For i = 0 To 11
r = (321 - 27 * i) Mod 17
If r < m
in Then min = r: a = i: b = (321 - 27 * i) / 17
Next i
Print r, a, b
End Sub
6. 小猴在一天摘了若干个桃子,当天吃掉一半多一个;第二天接着吃了剩下的桃子中的一半多一个,以
后每天都吃剩下桃子中的一半零一个,到第7天早上要吃时只剩下一个了,问小猴那天共摘下了多少个桃子?
Private Sub Command1_Click()
Dim i As Integer, s As Integer
s = 1
For i = 6 To 1 Step -1
s = (s + 1) * 2
Next i
Print s
End Sub
7. 某次大奖赛,有7个评委打分,满分为100分。对一名参赛者,输入7个评委的打分分数,去掉一个最高分、一个最低分后,求出平均分为该参赛者的得分。编写程序输入评委的打分,输出参赛者的得分。
Private Sub Command1_Click()
Dim Max As Integer, min As Integer
Dim i As Integer, s As Integer, f As Integer
Max = 0
min = 100
For i = 1 To 7
f = InputBox("请输入评委的打分")
If f > Max Then Max = f
If f < min Then min = f
s = s + f
Next i
aver = (s - Max - min) / 5
Print "参赛者最后得分为:"; aver, Max, min
End Sub
8. 输入任意长度的字符串,要求将字符顺序倒置,例如,将输入的“ABCDEFG”变换成“GFEDCBA”。
Private Sub Command1_Click()
Dim str1 As String, str2 As String
Dim i As Integer, k As Integer
str1 = InputBox("")
str2 = ""
k = Len(str1)
For i = k To 1 Step -1
str2 = str2 + Mid(str1, i, 1)
Next i
Print str2
End Sub
9. 由键盘输入一个正整数,出大于或等于该数的第一个质数。
Private Sub Command1_Click()
Dim i As Integer, x As Integer, j As Boolean
x = InputBox("请输入一个数")
Do While True
x = x + 1
For i = 2 To Sqr(x)
j = True
If x Mod i = 0 Then j = False: Exit For
Next i
If j Then Exit Do
Loop
Print x
End Sub
10. “完备数”是指一个数恰好等于它的因子之和,如6的因子为1、2、3,而6=1+2+3,因而6就是完备数。编程出1~1000之间的全部“完备数”。
Private Sub Command1_Click()
Dim i As Integer, a As Integer
Dim s As Integer
For i = 1 To 1000
a = 2: s = 1
Do While a < i
If i Mod a = 0 Then
s = s + a
End If
a = a + 1
Loop
If s = i Then
Print i
End If
Next i
End Sub
11. 勾股定理中三个数的关系是:a^2 + b^2 =c^2。编写程序,输出100以内满足上述关系的整数组合,例如3,4,5就是一个整数组合。
Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer
For a = 1 To 100
For b = a To 100
For c = b To 100
If a ^ 2 + b ^ 2 = c ^ 2 Then
Print "勾股定理整数组合"; a, b, c
End If
Next c
Next b
Next a
End Sub
12. 编写程序,打印如下的乘积表:
* 3 6 9 12
15
16
17
18
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Print " * 3 6 9 12"
For i = 15 To 18
Print i; " ";
For j = 3 To 12 Step 3
Print i * j; " ";
Next j
Print
Next i
End Sub
13. 一个两位正整数,如果将它的个位数字与十位数字对调,则产生另一个正整数,我们把后者叫做前
者的对调数。现给定一个两位正整数,请到另一个两位正整数,使得这两个两位正整数之和等于它们各自的对调数之和,若不到,则输出“查失败”。例如:32+12=21+23。(要求:在程序运行时从键盘输入给定的两位正整数)
Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer, d As Integer
Dim i As Integer, j As Integer, k As Integer, l As Integer
a = InputBox("")
i = a \ 10
j = a Mod 10
c = j * 10 + i
For b = 10 To 99
k = b \ 10
l = b Mod 10
d = l * 10 + k
If a + b = c + d Then Print a; "的对调数为"; b
Next b
End Sub
14. 有一数列:1、1、2、3、5、8、13、21…,求出这个数列的前20项之和。
Private Sub Command1_Click()
Dim f1 As Single, f2 As Single, f3 As Single
Dim i As Integer, j As Integer, n As Integer
Dim sum As Single
f1 = 1
f2 = 1
sum = 2
Do While i <= 20-3
f3 = f1 + f2
f1 = f2
f2 = f3
sum = sum + f3
i = i + 1
Loop
Print sum
End Sub
15. 把一个16进制数转换成一个10进制数。
Private Sub Command1_Click()
Dim a As Integer, k As String, h As Integer, s As Integer
a = InputBox("请输入一个16进制数")
k = Trim(Str(a) )
h = Len(k)
s = a Mod 10
For i = h-1 To 1 Step-1
s = s + Val(Mid(k, i, 1)) * 16 ^ (h-i)
Next i
Print "16进制数:"; a; "转换为10进制数是:"; s
End Sub
16. 编写程序,单击窗体时在窗体上打印出如下所示的图形。
*
***
*****
*******
*********
*******
*****
***
*
Private Sub Command1_Click()
Dim i As Integer, j As Integer
j = j + 1
For i = -4 To 4
Print Tab(20 + Abs(i));
For j = 1 To 9 -Abs(2 * i)
Print "*";
Next j
Print
Next i
End Sub
17. 编写程序,输出下列三角形: (要求用循环做,非循环不给分)
G
FG
EFG
DEFG
CDEFG
BCDEFG
ABCDEFG
Private Sub Command1_Click()
Dim a As String, b As String, i As Integer, j As Integer
Dim s As Integer, t As Integer, k As Integer, m As Integer
a = "ABCDEFG"
k = Len(a)
For i = 1 To k
Print Right(a, i)
Next i
End Sub
18. 编程输入一批数据,若输入数据为纯数字时,求累加和,非纯数字不累加
。输入"Y"结束输入,输出累加和。(用InputBox()输入数据)
Private Sub Command1_Click()
Dim x, s As Single, bz As String
Do While UCase(bz) < > "Y"
x = InputBox("请输入相加的值")
If Val(x) = x Then
s = s + x
End If
bz = InputBox("是否结束?(Y/N)")
Loop
Print s
End Sub
19. 求公式:e=1+1/1!+1/2!+1/3!+……+1/n! 直到n=20时的e值。
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim s As Single, h As Single, l As Single, k!
k = 1
s = 1
For i = 1 To 20
k = k * i
s = s + 1 / k
Next i
Print s
End Sub
20. 已知sin(x)=x/1-x^3/3!+x^5/5!-x^7/7!+…,设计一个程序,输入x,通过累加所有绝对值大于等于0.000001的项来计算sin(x)的值。
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim x As Single, sum As Single
Dim h As Single, k As Single
x = InputBox("请输入x的值")
h = 1
k = 1
Do
i = i + 1
h = h * x
k = k * i
If i Mod 2 = 1 Then
j = j + 1
sum = sum + (-1) ^ (j + 1) * h / k
End If
Loop While Abs(h / k) > 10 ^ (-6)
Print sum
End Sub
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论