版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、程序调试题1.计算1!+2!+.+20!Public Function nFactor(ByVal n As Integer) As Double '该过程用于计算n!。Dim i As Integer Dim temp As Double -1- temp=1 For i = 1 To n temp = temp * i Next i nFactor = -2- tempEnd Function Public Sub summary() '该过程用于计算1!+2!+.+20!,并打印出计算结果。Dim sum As Double Dim i As Integer D
2、im n As Integer n = 20 For i = 1 To n sum = sum +-3-nFactor (i) Next i Form1.Print "sum=" & -4- sumEnd Sub47Public Sub find() '该过程是用于从1到10000中找出这样的数, '该数各个位的数字的阶乘相加之和等于该数,并将结果输出。 Dim k, a, n, i Dim p As Integer For k = 1 To 10000 a = LTrim(Str(k) n = 0 For i= -1- 1 to a p = Val
3、(Mid(a, i, 1) n= -2- n+fact(p) Next i If n = k Then form1.Print k Next kEnd SubFunction fact(x As Integer) As Long '该函数用于计算阶乘 Dim y As Long Dim i% y = 1 For i% = 1 To x y = y * i% Next i% -3- fact=yEnd Function2. -统计个位上的数字Public Sub calculate() Dim a(1 To 100) As Integer Dim x(1 To 10) As Intege
4、r Dim i As Integer, p As Integer '产生100个0,99范围内的随机整数 '每行10个打印出来 For i = 1 To 100 '* 错误1 * a(i) = Rnd * 99 a(i)=int(Rnd*100) If a(i) < 10 Then Form1.Print Space(2); a(i); Else Form1.Print Space(1); a(i); End If If i Mod 10 = 0 Then Form1.Print Next i '统计个位上的数字分别为1,2,3,4,5,6,7,8,9,0
5、的数的个数 '并将统计结果保存在数组x(1),x(2),.,x(10)中,将统计结果打印出来 For i = 1 To 100 '* 错误2 * p = Int(a(i) / 10) '求个位上的数字 p=a(i) mod 10 If p = 0 Then p = 10 '* 错误3 * x(p) = x(p) 1 x(p)=x(p)+1 Next i Form1.Print "统计结果" For i = 1 To 10 p = i If i = 10 Then p = 0 '* 错误4 * Form1.Print "个位数
6、为" + Str(p) + "共" + Str(x(p) + "个" str(x(p)改成str(x(i) Next iEnd Sub3. 在字符串str1中查找"at"Public Sub Findat() '在字符串str1中查找"at" Dim str1 As String Dim length As Integer '字符串长度 Dim sum As Integer '查到的个数 Dim i As Integer str1 = InputBox("请输入一个字符串
7、") -1- length =len(str1) i = 1 sum = 0 Do While -2- i <=length If -3-Thenmid(str1,i,2) = "at" sum = sum + 1 End If i = i + 1 Loop If -4-Thensum=0 MsgBox "没有找到!" Else MsgBox "找到了" & Str(sum) & "个" End IfEnd Sub4. -'该过程是猜数游戏,由计算机产生一个1,100的任意整
8、数,'输入猜数后计算机给出提示,如果5次后还没有猜中就结束游戏并公布正确答案。Public Sub Guess() Dim R As Integer Dim X As Integer Dim times As Integer Randomize '* 错误1 * R = Rnd * 100 '产生一个1-100 的任意整数 R=int(Rnd*100)+1 times = 1 Do X = Val(InputBox("输入猜数X") Select Case X Case R Form1.Print "猜中了" Exit Do
9、39;* 错误2 * Case X > R case is>R Form1.Print "太大了,继续猜!" Case Else Form1.Print "太小了,继续猜!" End Select times = times + 1 '* 错误3 * Loop While times > 5 Loop Until time>5 If times > 5 Then Form1.Print "猜数失败,游戏结束!" '* 错误4 * Form1.Print "正确答案为"
10、& Str(X) Str(R) End IfEnd Sub5. -'该过程是用于求出100-999之间所有的Armstrong数,并输出。 '所谓Armstrong数是指一个数等于它每位上数字的立方和。例如:371=33+73+13,那么371就是一个Armstrong数。Public Sub ArmstrongNumber() Dim armstrong As Integer Dim i As Integer Dim hundred As Integer '百位上的数字 Dim ten As Integer '十位上的数字 Dim one As Int
11、eger '个位上的数字 For i = 100 To 999 -1- hundred = i100 -2- ten = (I Mod 100)10 -3- one = I Mod 10 If -4- Then I=hundred3+ten3+one3 Form1.Print i; "is armstrong number" End If Next iEnd Sub6. -Public Sub CountTo60() '从一堆一分、二分、五分的硬币中取出20枚,使其总值为60分 '输出取法的数量及每一种取法的一分、二分、五分的个数 Dim one A
12、s Integer '一分硬币个数 Dim two As Integer '二分硬币个数 Dim five As Integer '五分硬币个数 Const n = 20 '总数20枚 Dim k As Integer '取法数量 k = 0 For one = 1 To n '* 错误1 * For two = one + 1 To n For two = 1 To n-one five = n - one - two '* 错误2 * If one + two + five = 60 Then If one + two*2 + fiv
13、e*5 = 60 Then k = k + 1 Form1.Print "one=" one; "two=" two; "five=" five End If '* 错误3 * Next one Next two '* 错误4 * Next two Next one Form1.Print "k=" kEnd Sub7. -Private Const n = 10Private a(1 To n) As Integer'TJ过程是将一批数据中小于零的数及它们的积打印出来'数据由SCS
14、J过程产生,数据的取值范围为-10 10 之间的随机整数数Public Sub TJ() Dim i As Integer Dim t As Single - 1 - t=1 For i = 1 To 10 If - 2 - Then a(i)<0 t = t * a(i) End If Next i Form1.Print "T=" tEnd SubPublic Sub SCSJ() Randomize Form1.Print "原始数据" Dim i As Integer Dim j As Integer For i = 1 To n '
15、;随机产生0或1,为0时取负,为1时取正 j = Int(Rnd * 2) If - 3 - Then J = -1 j=0 a(i) = j * Int(Rnd * (n + 1) Form1.Print a(i); Next i Form1.PrintEnd Sub8. -餐厅发工资,总计要给7个人发工资统计100元、50元、10元、5元和1元的票面数量和总数量Dim salary(7) As IntegerPublic Sub Money() Dim hundred As Integer, totalhundred As Integer '100元票面数量、总数量 Dim fif
16、ty As Integer, totalfifty As Integer '50元票面数量、总数量 Dim ten As Integer, totalten As Integer '10元票面数量、总数量 Dim five As Integer, totalfive As Integer '5元票面数量、总数量 Dim one As Integer, totalone As Integer '1元票面数量、总数量 Dim totalsalary As Integer '工资总计 Dim i As Integer, temp As Integer tota
17、lhundred = 0 totalfifty = 0 totalten = 0 totalfive = 0 totalone = 0 totalsalary = 0 For i = 1 To 7 -1- temp = salary (i) hundred = Int(temp / 100) -2- temp = salary (i) Mod 100 fifty = Int(temp / 50) temp = temp - fifty * 50 ten = Int(temp / 10) temp = temp - ten * 10 five = Int(temp / 5) temp = tem
18、p - five * 5 -3- one = temp totalhundred = totalhundred + hundred totalfifty = totalfifty + fifty totalten = totalten + ten totalfive = totalfive + five totalone = totalone + one -4- totalsalary = totalfifty + totalten+ totalfive+ totalone Next i Form1.Print "共需100元" & Str(totalhundred
19、) & "张" Form1.Print "共需50元" & Str(totalfifty) & "张" Form1.Print "共需10元" & Str(totalten) & "张" Form1.Print "共需5元" & Str(totalfive) & "张" Form1.Print "共需1元" & Str(totalone) & "张&quo
20、t; Form1.Print "合计" & Str(totalsalary) & "元"End Sub Public Sub SalaryData() salary(1) = 1398 salary(2) = 1765 salary(3) = 689 salary(4) = 1500 salary(5) = 832 salary(6) = 590 salary(7) = 1200End Sub9. -DataProduce过程用于产生5*4的数组MaxLine过程用于计算最大平均值所在行,并交换第一行与最大平均值所在行Avera
21、ge过程用于计算各行的平均值PrintArray过程用于打印5*4数组和每行的平均值Dim a(1 To 5, 1 To 4) As IntegerDim ave(1 To 5) As IntegerPublic Sub MaxLine() Dim i As Integer Dim j As Integer Dim temp As Integer Dim Line_no As Integer '最大平均值的行号 '找出最大平均值所在行 Line_no = 1 For i = 2 To 5 '* 错误1 * If ave(Line_no) >= ave(i) The
22、n ave(Line_no) < ave(i) '* 错误2 * Line_no = ave(i) Line_no = i End If Next i '交换第一行与最大平均值所在行 For j = 1 To 4 temp = a(1, j) '* 错误3 * a(1, j)= a (Line_no, j) a(Line_no, j) = a(1, j) a(Line_no, j) = temp Next j '交换对应行的平均值 temp = ave(1) ave(1) = ave(Line_no) ave(Line_no) = temp '打印
23、交换后的数据 Form1.Print "交换后的数据和平均值" PrintArrayEnd SubPublic Sub DataProduce() Dim i As Integer Dim j As Integer '产生5*4数组 For i = 1 To 5 For j = 1 To 4 a(i, j) = Int(100 * Rnd) Next j Next iEnd SubPublic Sub Average() Dim i As Integer Dim j As Integer Dim sum As Integer '计算各行平均值 For i =
24、 1 To 5 sum = 0 For j = 1 To 4 '* 错误4 * sum = a(i, j) sum = sum +a(i, j) Next j ave(i) = sum / 4 Next i '将数组和平均值打印出来 Form1.Print "原始数据和平均值" PrintArrayEnd SubPublic Sub PrintArray() Dim i As Integer Dim j As Integer For i = 1 To 5 For j = 1 To 4 Form1.Print a(i, j); Spc(3); Next j F
25、orm1.Print "平均值=" ave(i) Next iEnd Sub10. -Public Sub Wrap()'该过程用于判断一个字符串是否“回文”。'所谓“回文”是指字符串顺读与倒读都是一样的,如“潮起潮落,落潮起潮”。 Dim length As Integer Dim str1 As String Dim strleft As String Dim strright As String Dim k As Integer str1 = InputBox("请输入任意的字符串") '输入任意字符串 '*错误1*
26、 length = Val(str1) length = Len(str1) k = 1 Do '*错误2* strleft = Left(str1, k) '从左边起逐个取出一个字符 strleft = Mid(str1, k,1) '*错误3* strright = Right(str1, k) '从右边起逐个取出一个字符 strright = Mid(str1,length- k+1,1) '*错误4* If strleft = strright Then strleft<> strright Exit Do End If k = k
27、+ 1 Loop While k <= length / 2 If k > length / 2 Then Form1.Print str1 & "是回文" Else Form1.Print str1 & "不是回文" End IfEnd Sub11. -'BubbleSort过程是冒泡排序'Swap过程交换两个变量数值'GenerateData过程产生10个1,20之间互不相同的随机整数Private Const n = 10Private a(1 To n) As IntegerPublic Sub
28、 BubbleSort() Dim i As Integer, j As Integer i = 1 Do For j = i + 1 To n If a(j) > a(i) Then '* 1 * Swap (a(j), a(i) Call Swap (a(j), a(i) End If Next j i = i + 1 '* 2 * Loop While i = n I<= n Form1.Print "排序结果" For i = 1 To n Form1.Print a(i); Next iEnd SubPublic Sub Generat
29、eData() Dim i As Integer Dim j As Integer Dim b As Boolean For i = 1 To n b = False Do While Not b a(i) = Int(20 * Rnd + 1) b = True '* 3 * For j = 1 To I for j = 1 To I-1 If a(i) = a(j) Then b = False Exit For End If Next j Loop Form1.Print a(i) Next iEnd Sub'* 4 *Public Sub Swap(ByVal a As
30、 Integer, ByVal b As Integer) sub Swap(a As Integer, b As Integer) Dim temp As Integer temp = a a = b b = tempEnd Sub12. -十六进制数转换成十进制数Public Sub Transfer() Dim Hex As String '十六进制数 Dim Dec As Double '十进制数 Dim temp As String Dim i As Integer Dim n As Integer Hex = InputBox("输入一个十六进制整数&qu
31、ot;) '* 错误1 * n = Val(Hex) n = Len(Hex) i = 0 Do '* 错误2 * temp = Mid(Hex, i, 1) temp = Mid(Hex, n-i, 1) '* 错误3 * Dec = Dec + number * 16 I Dec = Dec + number(temp) * 16 i i = i + 1 Loop While i < n '* 错误4 *Form1.Print str(Hex) & "转换为十进制数为" & str(Dec) Hex & En
32、d SubPublic Function number(str As String) As Integer Select Case str Case "a", "A" number = 10 Case "b", "B" number = 11 Case "c", "C" number = 12 Case "d", "D" number = 13 Case "e", "E" number = 14
33、Case "f", "F" number = 15 Case Else number = Val(str) End SelectEnd Function13. -Public Sub SortData() '产生10个30,50的随机整数,并按从小到大的顺序打印出来 Dim a(10) As Integer Dim i As Integer, j As Integer For i = 1 To 10 - 1 - a(i)= Int(Rnd*21)+30 Next i For i = 1 To 9 - 2 - For j=I+1 To 10 If
34、a(i) > a(j) Then - 3 - Swap a(i),a(j)或call swap (a(i),a(j) End If Next j Form1.Print a(i) Next i - 4 - Form1.print a(10)End Sub Public Sub Swap(a As Integer, b As Integer) Dim temp As Integer temp = a a = b b = tempEnd Sub14. -Public Sub PrintArray() '下面程序用于打印5行5列的数字方阵 '使两对角线上元素均为1,其
35、余均为2 '要求打印的数字方阵两列数字之间空3格,两行之间空一行 Dim a(5, 5) As Integer Dim i As Integer, j As Integer For i = 1 To 5 For j = 1 To 5 '* 错误1 * If i <> j Or i <> 6 - j Then or 改成And a(i, j) = 2 Else a(i, j) = 1 End If '* 错误2 * Form1.Print a(i, j) + Space(3); Form1.Print a(i, j) ; Space(3); Nex
36、t j '* 错误3 * Form1.Print Form1.Print: Form1.Print Next iEnd Sub15. -Public Sub Combination() '该过程是用于计算在m个数据中取出n个数据的排列组合值计算公式为Cmn=m!/(n!*(m-n)!)。 Dim m As Integer Dim n As Integer Dim Cmn As Long Do m = Val(InputBox("请输入一个整数m") n = Val(InputBox("请输入一个整数n(n<=m)") Loop Wh
37、ile m < n '必须保证输入的两个数m>=n '* 错误1 * Cmn = nFactor(m) / nFactor(n) * nFactor(m - n) 改成Cmn = nFactor(m) /( nFactor(n) * nFactor(m - n) Form1.Print "排列组合数为" CmnEnd SubPublic Function nFactor(ByVal n As Integer) As Double '该函数过程用于计算n!。 Dim i As Integer Dim temp As Double temp
38、= 1 For i = 1 To n temp = temp * i Next i '*错误 2 * nFactor(n) = temp nFactor= temp '* 错误3 * End Sub End Function16. -'该过程是用于查找一批数据中的最大数,并输出最大数的值及最大数在数组中的下标值。Dim a(30) As IntegerPublic Sub FindMax() Dim Position As Integer '最大数在数组中的下标 Dim max As Integer '最大数 Dim i As Integer Gener
39、ateData max = -1- a(1) Position = -2- 1 For i = 2 To 30 If a(i) > max Then max = -3- a(i) Position = -4- I End If Next i Form1.Print "最大值=" & Str(max) Form1.Print "最大值的下标=" & Str(Position)End SubPublic Sub GenerateData() Dim i As Integer For i = 1 To 30 a(i) = Int(500
40、* Rnd + 1) Form1.Print a(i), If i Mod 5 = 0 Then Form1.Print Next iEnd Sub17. -Public Sub prime() '打印出100到200之间所有的素数,并统计素数的个数 Dim i As Integer Dim j As Integer Dim k As Integer Dim t As Integer '统计素数的个数 Dim b As Boolean For i = 100 To 200 b = True - 1 - k=2 j = Int(Sqr(i) Do While k <= j And b If i Mod k = 0 Then - 2 - b=False End I
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025机械设备的买卖合同
- 洛阳理工学院《工科大学化学-物理化学(二)》2023-2024学年第一学期期末试卷
- 污水处理厂导向钻进施工合同
- 墙绘施工合同范本
- 教育培训机构劳务管理
- 食品企业财务健康检查
- 2024年动力煤进口清关共享成功之道!3篇
- 广西壮族自治区河池市2023-2024学年高一上学期1月期末考试数学试题(解析版)
- 医疗器械招投标管理规范
- 医药招投标项目招标文件编制
- 国家开放大学电大《建筑制图基础》机考三套标准题库及答案3
- 降低故障工单回复不合格率
- 可涂色简笔画打印(共20页)
- 灯光架介绍及使用说明
- 十一学校行动纲要
- GB 1886.6-2016 食品安全国家标准 食品添加剂 硫酸钙(高清版)
- 关于房屋征收及土地收储过程中的税收政策(仅供参考)
- 唯一住房补贴申请书(共2页)
- 单面多轴钻孔组合机床动力滑台液压系统课程设计
- 中医养生脾胃为先PPT文档
- 门窗工程成品保护方案(附图)
评论
0/150
提交评论