《程序设计基础——Visual Basic学习与实验指导 教学课件 ppt 作者 陈丽芳习题答案 第8章实验答案》由会员分享,可在线阅读,更多相关《程序设计基础——Visual Basic学习与实验指导 教学课件 ppt 作者 陈丽芳习题答案 第8章实验答案(6页珍藏版)》请在金锄头文库上搜索。
1、实验8-1 有一个阶梯,如果每步跨2阶,最后余1阶;每步跨3阶,最后余2阶;每步跨5阶,最后余4阶;每步跨6阶,最后余5阶;每步跨7阶,正好到达阶梯顶。问阶梯至少有多少阶?(1)Private Sub Form_Click()Dim m As IntegerDo If m Mod 2 = 1 And m Mod 3 = 2 And m Mod 4 = 3 And m Mod 5 = 4 And m Mod 6 = 5 And m Mod 7 = 0 Then Print m Exit Do End If m = m + 1LoopEnd Sub(2)Private Sub Form_Click
2、()Dim m As Integer, n As IntegerFor n = 7 To 32767 Step 7 n整数,循环的最大值可以采用32767 If n Mod 2 = 1 And n Mod 3 = 2 And n Mod 4 = 3 And n Mod 5 = 4 And n Mod 6 = 5 Then Print n Exit For End IfNext nEnd Sub实验8-2 利用循环结构显示如图8-4所示的界面。图8-4 程序运行界面图(1)Private Sub Form_Click()For i = 1 To 9 Print Tab(10 - i); Stri
3、ng(2 * i - 1, Trim(Str(i)Next iEnd Sub(2)Private Sub Form_Click()For i = 1 To 9 Print Tab(10 - i); For m = 1 To 2 * i - 1 Print Trim(Str(i); Next m PrintNext iEnd SubPrivate Sub Form_Click()For i = 9 To 1 Step -1 Print Tab(10 - i); String(2 * i - 1, Chr(64 + 10 - i)Next iEnd SubPrivate Sub Form_Clic
4、k()For i = 1 To 9 Print Tab(10 - i); String(2 * i - 1, Trim(Str(i)Next iFor i = 8 To 1 Step -1 Print Tab(10 - i); String(2 * i - 1, Trim(i)Next iEnd Sub实验8-3 求sn=a+aa+aaa+aaaa+aaaaa(n个a),其中a是一个由随机数产生的19(包括1、9)中的一个正整数,n是一个由随机数产生的510(包括5、10)中的一个数。Private Sub Form_Click()Dim sn!, w!, i%, a%, n%Randomiz
5、ea = Int(Rnd * 9 + 1)n = Int(Rnd * 6 + 5)sn = 0: w = 0Print a=; a, n=; nPrint sn=;For i = 1 To n w = w * 10 + a sn = sn + w If i = n Then Print w; Else Print w; +;Next iPrint =; snEnd Sub实验8-4 在窗体上显示出所有的水仙花数。水仙花数是指一个3位数,其中各位数字立方和等于该数字本身。例如,153=13+53+33,所以153就是水仙花数。(1)Private Sub Form_Click()Dim m As
6、 Integer, a%, b%, c%, n%n = 0For m = 100 To 999 Step 1 a = m 100 b = (m - a * 100) 10 十位数上的数计算有多种表示方法,请同学思考 c = m Mod 10 If a 3 + b 3 + c 3 = m Then Print m; n = n + 1 If n Mod 5 = 0 Then Print End IfNext mEnd Sub(2)Private Sub Form_Click()Dim a%, b%, c%, m%For a = 1 To 9 For b = 0 To 9 For c = 0 To
7、 9 m = a * 100 + b * 10 + c If a 3 + b 3 + c 3 = m Then Print m; Next c Next bNext aEnd Sub(3)Private Sub Form_Click()Dim m As Integer, a%, b%, c%, n%Dim str As Stringn = 0For m = 100 To 999 Step 1 str = CStr(m) a = Val(Mid(str, 1, 1) b = Val(Mid(str, 2, 1) c = Val(Mid(str, 3, 1) If a 3 + b 3 + c 3
8、= m Then Print m; n = n + 1 If n Mod 5 = 0 Then Print End IfNext mEnd Sub(4)思考题Private Sub Form_Click()Dim i&, n%, s&, m%For i = 1 To 99999 n = Len(CStr(i) s = 0 For j = 1 To n s = s + Mid(CStr(i), j, 1) n Next j If s = i Then Print i; m = m + 1 If m Mod 5 = 0 Then Print End IfNext iEnd Sub实验8-5 使用下
9、面的级数公式求的值,当计算到某一项式的绝对值小于0.0001时,认为满足精度,停止计算。Private Sub Command1_Click()Dim a As Double, m As Double, s As Integer, i As Longs = 1i = 1Dom = 1 / ia = a + s * mi = i + 2s = -sLoop While m = 0.00001Text1.Text = 4 * aEnd Sub实验8-6 编程找出10000以内所有的回文数的平方仍是回文数的数。实现功能:(1)单击“生成数据”按钮,应将10000以内的所有的回文数的平方仍是回文数的数
10、按“112=121”形式存放在列表框中。(2)单击“返回”按钮,结束程序运行。回文数是指左右数字完全对称的自然数。例如,11、121、1221等都是回文数(请仿照实验7-10,应用穷举算法,自行完成程序的编写)。Private Sub Command1_Click()Dim m As LongDim i As Integer, i1 As Integer, n As Integer, n1 As IntegerDim str As String, str1 As StringFor m = 10 To 9999 str = CStr(m) str1 = CStr(m 2) n = Len(st
11、r) n1 = Len(str1) For i = 1 To n If Mid(str, i, 1) Mid(str, n - i + 1, 1) Then Exit For End If Next i For i1 = 1 To n1 If Mid(str1, i1, 1) Mid(str1, n1 - i1 + 1, 1) Then Exit For End If Next i1 If i n And i1 n1 Then List1.AddItem (str & 2= & str1) End IfNext mEnd SubPrivate Sub Command2_Click()EndEn
12、d Sub实验8-7 编程计算的值,其中n=20。Private Sub Form_Click()Dim s As DoubleDim an As DoubleDim n As Integers = 0For n = 1 To 20 an = 1 / (n * (n + 1) s = s + anNext nPrint s=; s;End Sub实验8-8 已知x、y、z分别是09中的一个数,求x、y、z的值,使得xxz+yzz=532成立(其中xxz和yzz不表示乘积,而是由x、y、z组成的三位数)。Private Sub Form_Click()Dim x%, y%, z%Dim m As Integer, n As IntegerFor x = 1 To 9 For y = 1 To 9 For z = 0 To 9 n = x * 100 + x * 10 + z m = y * 100 + z * 10 + z If n + m = 532 Then Print n; m End If Next z Next yNext x End Sub