VB循环程序例题100道附答案(29页).doc

上传人:1595****071 文档编号:35423420 上传时间:2022-08-21 格式:DOC 页数:29 大小:247.50KB
返回 下载 相关 举报
VB循环程序例题100道附答案(29页).doc_第1页
第1页 / 共29页
VB循环程序例题100道附答案(29页).doc_第2页
第2页 / 共29页
点击查看更多>>
资源描述

《VB循环程序例题100道附答案(29页).doc》由会员分享,可在线阅读,更多相关《VB循环程序例题100道附答案(29页).doc(29页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、-VB循环程序例题100道附答案-第 29 页VB循环程序例题附答案 1.素数1. 100,999范围内同时满足以下两个条件的十进制数. 其个位数字与十位数字之和除以10所得的余数是百位数字;该数是素数; 求有多少个这样的数? 15n = 0For i = 100 To 999a = i 100b = i 10 Mod 10c = i Mod 10If (c + b) Mod 10 = a Then x = 0 For j = 2 To i - 1 If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then n = n + 1End IfN

2、ext iPrint n2. 300,800范围内同时满足以下两个条件的十进制数. 其个位数字与十位数字之和除以10所得的余数是百位数字 ;该数是素数;求满足上述条件的最大的三位十进制数。761For i = 300 To 800a = i 100b = i 10 Mod 10c = i Mod 10If (c + b) Mod 10 = a Then x = 0 For j = 2 To i - 1 If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then s = iEnd IfNext iPrint s3. 除1和它本身外,不能被其它

3、整数整除的正整数称为素数(注:1不是素数,2是素数)。若两素数之差为2 ,则称两素数为双胞胎数,问31,601之间有多少对双胞胎数。22n = 0For i = 31 To 601 - 2 x = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then y = 0 For j = 2 To Sqr(i + 2) If (i + 2) Mod j = 0 Then y = 1: Exit For Next j If y = 0 Then n = n + 1 End IfNext iPrint n

4、4.数学家哥德巴赫曾猜测:任何大于6的偶数都可以分解成两个素数(素数对)的和。但有些偶数可以分解成多种素数对的和,如: 10=3+7,10=5+5,即10可以分解成两种不同的素数对。试求6744可以分解成多少种不同的素数对(注: A+B与B+A认为是相同素数对)144n = 0For i = 2 To 6744 / 2 x = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then y = 0 For j = 2 To Sqr(6744 - i) If (6744 - i) Mod j = 0

5、 Then y = 1: Exit For Next j If y = 0 Then n = n + 1 End IfNext iPrint n5. 两个素数之差为2,则称这两个素数为双胞胎数。求出200,1000之间的最大一对双胞胎数的和。1764For i = 200 To 1000 - 2 x = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then y = 0 For j = 2 To Sqr(i + 2) If (i + 2) Mod j = 0 Then y = 1: Exit

6、For Next j If y = 0 Then s = i + i + 2 End IfNext iPrint s6. 一个素数(设为p)依次从最高位去掉一位,二位,三位,若得到的各数仍都是素数(注:除1和它本身外,不能被其它整数整除的正整数称为素数,1不是素数,2是素数),且数p的各位数字均不为零,则称该数p为逆向超级素数。例如,617,17,7都是素数,因此617是逆向超级素数,尽管503,03,3都是素数,但它不是逆向超级素数,因为它包含有零。试求100,999之内的所有逆向超级素数的个数。 39n = 0For i = 100 To 999 a = i 10 Mod 10 b = i

7、 Mod 10 If a 0 And b 0 And b 1 Then x = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then y = 0 c = i Mod 100 For j = 2 To Sqr(c) If c Mod j = 0 Then y = 1: Exit For Next j If y = 0 Then z = 0 For j = 2 To Sqr(b) If b Mod j = 0 Then z = 1: Exit For Next j If z = 0 Then n

8、 = n + 1 End If End If End IfNext iPrint n7.德国数学家哥德巴赫曾猜测:任何大于6的偶数都可以分解成两个素数(素数对)的和。但有些偶数可以分解成多种素数对的和,如: 10=3+7,10=5+5,即10可以分解成两种不同的素数对。试求1234可以分解成多少种不同的素数对(注: A+B与B+A认为是相同素数对)25n = 0For i = 2 To 1234 / 2 x = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then y = 0 For j =

9、 2 To Sqr(1234 - i) If (1234 - i) Mod j = 0 Then y = 1: Exit For Next j If y = 0 Then n = n + 1 End IfNext iPrint n8.求100,900之间相差为12的素数对(注:要求素数对的两个素数均在该范围内)的个数。50n = 0For i = 100 To 900 - 12 x = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then y = 0 For j = 2 To Sqr(i +

10、 12) If (i + 12) Mod j = 0 Then y = 1: Exit For Next j If y = 0 Then n = n + 1 End IfNext iPrint n9.一个素数(设为p)依次从最高位去掉一位,二位,三位,若得到的各数仍都是素数(注:1不是素数),且数p的各位数字均不为零,则称该数p为逆向超级素数。例如,617,17,7都是素数,因此617是逆向超级素数,但尽管503,03,3都是素数,但它不是逆向超级素数,因为它包含有零。试求100,999之内的所有逆向超级素数的和。21645s = 0For i = 100 To 999 a = i 10 Mo

11、d 10 b = i Mod 10 If a 0 And b 0 And b 1 Then x = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then y = 0 c = i Mod 100 For j = 2 To Sqr(c) If c Mod j = 0 Then y = 1: Exit For Next j If y = 0 Then z = 0 For j = 2 To Sqr(b) If b Mod j = 0 Then z = 1: Exit For Next j If z

12、= 0 Then s = s + i End If End If End IfNext iPrint s10.一个素数(设为p)依次从最高位去掉一位,二位,三位,若得到的各数仍都是素数(注:1不是素数,2是素数),且数p的各位数字均不为零,则称该数p为逆向超级素数。例如,617,17,7都是素数,因此617是逆向超级素数,但尽管503,03,3都是素数,但它不是逆向超级素数,因为它包含有零。试求100,999之内的所有逆向超级素数从大到小数的第10个素数是多少?797n = 0For i = 999 To 100 Step -1 a = i 10 Mod 10 b = i Mod 10 If

13、a 0 And b 0 And b 1 Then x = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then y = 0 c = i Mod 100 For j = 2 To Sqr(c) If c Mod j = 0 Then y = 1: Exit For Next j If y = 0 Then z = 0 For j = 2 To Sqr(b) If b Mod j = 0 Then z = 1: Exit For Next j If z = 0 Then n = n + 1: I

14、f n = 10 Then s = i End If End If End IfNext iPrint s11.一个自然数是素数,且它的数字位置经过任意对换后仍为素数,则称为绝对素数。如13,试求所有两位绝对素数的和。429s = 0For i = 10 To 99 a = i 10 b = i Mod 10 x = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then y = 0 c = b * 10 + a For j = 2 To Sqr(c) If c Mod j = 0 Then

15、y = 1: Exit For Next j If y = 0 Then s = s + i End IfNext iPrint s12.在200,900范围 内同时满足以下两个条件的十进制数:其个位数字与十位数字之和除以10所得的余数是百位数字; 该数是素数;问有多少个这样的数?14n = 0For i = 200 To 900a = i 100b = i 10 Mod 10c = i Mod 10If (c + b) Mod 10 = a Then x = 0 For j = 2 To i - 1 If i Mod j = 0 Then x = 1: Exit For Next j If

16、x = 0 Then n = n + 1End IfNext iPrint n13.一个素数,依次从个位开始去掉一位,二位.,所得的各数仍然是素数,称为超级素数。求100,999之内超级素数的个数。14n = 0For i = 100 To 999 x = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then a = i 10 y = 0 For j = 2 To Sqr(a) If a Mod j = 0 Then y = 1: Exit For Next j If y = 0 Then

17、b = i 100 If b 1 Then z = 0 For j = 2 To Sqr(b) If b Mod j = 0 Then z = 1: Exit For Next j If z = 0 Then n = n + 1 End If End If End IfNext iPrint n14. 若两个连续的自然数的乘积减1后是素数,则称此两个连续自然数为友数对,该素数称为友素数。例如,由于 8*9-1=71, 因此,8与9是友素数对,71是友素数。求100,200之间的第10个友素数对所对应的友素数的值(按由小到大排列)。17291For i = 100 To 199 a = i *

18、(i + 1) - 1 x = 0 For j = 2 To Sqr(a) If a Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then n = n + 1 If n = 10 Then s = a End IfNext iPrint s15.求2,400中相差为10的相邻素数对(两数中间没有其它素数)的对数。5n = 0For I = 2 To 400 - 10 x = 0 For j = 2 To Sqr(I) If I Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then y = 0 F

19、or j = 2 To Sqr(I + 10) If (I + 10) Mod j = 0 Then y = 1: Exit For Next j If y = 0 Then For k = I + 1 To I + 9 z = 0 For k1 = 2 To Sqr(k) If k Mod k1 = 0 Then z = 1: Exit For Next k1 If z = 0 Then Exit For Next k If k = I + 10 Then n = n + 1 End If End IfNext IPrint n16.若两个连续的自然数的乘积减1后是素数,则称此两个连续自然数

20、为友数对,该素数称为友素数。例如,由于 8*9-1=71, 因此,8与9是友数对,71是友素数。求50,150之间的友数对的数目。38n = 0For i = 50 To 150 a = i * (i + 1) - 1 x = 0 For j = 2 To Sqr(a) If a Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then n = n + 1 End IfNext iPrint n17. 若两个连续自然数乘积减1后是素数,则称此两个自然连续数为友数对,该素数称为友素数,例:2*3-1=5,因此2与3是友数对,5是友素数,求40,119

21、之间友素数对的数目。30n = 0For i = 40 To 119 a = i * (i + 1) - 1 x = 0 For j = 2 To Sqr(a) If a Mod j = 0 Then x = 1: Exit For Next j If x = 0 Then n = n + 1 End IfNext iPrint n18.梅森尼数是指能使2n-1为素数的数n,求1,21范围内有多少个梅森尼数?7n = 0For i = 2 To 21 x = 2 i - 1 k = 0 For j = 2 To Sqr(x) If x Mod j = 0 Then k = 1 Next j I

22、f k = 0 Then n = n + 1Next iPrint n19. 300,800范围内同时满足以下两个条件的十进制数. 其个位数字与十位数字之和除以10所得的余数是百位数字 ;该数是素数;求满足上述条件的最大的三位十进制数。761For i = 300 To 800 a = i 100 b = i 10 Mod 10 c = i Mod 10 If (c + b) Mod 10 = a Then k = 0 For j = 2 To Sqr(i) If i Mod j = 0 Then k = 1 Next j If k = 0 Then s = i End IfNext iPri

23、nt s 2. 取数字20. 求符合下列条件的四位完全平方数(某个正整数A是另一个正整数B的平方,则称A为完全平方数),它的千位数字与十位数字之和等于百位数字与个位数字之积,例如,3136=562, 且3+3=1*6 故3136是所求的四位完全平方数. 求其中最大的一个数。7921For i = 1000 To 9999 x = Sqr(i) If x = Int(x) Then a = i 1000 b = i 100 Mod 10 c = i 10 Mod 10 d = i Mod 10 If (a + c) = b * d Then s = i End IfNext iPrint s21

24、.设某四位数的千位数字平方与十位数字的平方之和等于百位数字的立方与个位数字的立方之和,例如,对于四位数:3201, 32+02=23+13,试问所有这样的四位数之和是多少?97993s = 0For i = 1000 To 9999 a = i 1000 b = i 100 Mod 10 c = i 10 Mod 10 d = i Mod 10 If (a 2 + c 2) = b 3 + d 3 Then s = s + iNext iPrint s22. 设某四位数的千位数字与十位数字的和等于百位数字与个位数字的积,例如,对于四位数:9512,9+1=5*2,试问所有这样的四位数之和是多少

25、?1078289s = 0For i = 1000 To 9999 a = i 1000 b = i 100 Mod 10 c = i 10 Mod 10 d = i Mod 10 If (a + c) = b * d Then s = s + iNext iPrint s23.有一个三位数满足下列条件: (1)此三位数的三位数字各不相同; (2)此三位数等于它的各位数字的立方和。试求所有这样的三位数之和。1301s = 0For i = 100 To 999 a = i 100 b = i 10 Mod 10 c = i Mod 10 If (a 3 + b 3 + c 3) = i And

26、 a b And b c And c a Then s = s + iNext iPrint s24. 求1,999之间能被3整除,且至少有一位数字是5的所有正整数的个数。91n = 0For i = 1 To 999 a = i 100 b = i 10 Mod 10 c = i Mod 10 If i Mod 3 = 0 And (a = 5 Or b = 5 Or c = 5) Then n = n + 1Next iPrint n25. 有一个三位数满足下列条件: (1)此三位数的三位数字各不相同; (2)此三位数等于它的各位数字的立方和。试求所有这样的三位数中最大的一个是多少? 40

27、7s = 0For i = 100 To 999 a = i 100 b = i 10 Mod 10 c = i Mod 10 If (a 3 + b 3 + c 3) = i And a b And b c And c a Then s = iNext iPrint s26. 有一个三位数满足下列条件: (1)此三位数的三位数字各不相同; (2)此三位数等于它的各位数字的立方和。试求这种三位数共有多少个? 4n = 0For i = 100 To 999 a = i 100 b = i 10 Mod 10 c = i Mod 10 If (a 3 + b 3 + c 3) = i And a

28、 b And b c And c a Then n = n + 1Next iPrint n27.求五位数各位数字的平方和为100的最大的五位数。94111For i = 10000 To 99999 a = i 10000 b = i 1000 Mod 10 c = i 100 Mod 10 d = i 10 Mod 10 e = i Mod 10 If a 2 + b 2 + c 2 + d 2 + e 2 = 100 Then s = iNext iPrint s28.所谓“水仙花数”是指一个三位数,其各位数字的三次方之和等于该数本身,例如:153=13+33+53,故153是水仙花数,

29、求100,999之间所有水仙花数之和。1301s = 0For i = 100 To 999 a = i 100 b = i 10 Mod 10 c = i Mod 10 If (a 3 + b 3 + c 3) = i Then s = s + iNext iPrint s29. 设某四位数的各位数字的平方和等于100,问共有多少个这种四位数?49n = 0For i = 1000 To 9999 a = i 1000 b = i 100 Mod 10 c = i 10 Mod 10 d = i Mod 10 If a 2 + b 2 + c 2 + d 2 = 100 Then n = n

30、 + 1Next iPrint n30. 回文数是指正读和反读都一样的正整数。例如3773是回文数。求出1000,9999以内的所有回文数的个数。90n = 0For i = 1000 To 9999 a = i 1000 b = i 100 Mod 10 c = i 10 Mod 10 d = i Mod 10 If a + b * 10 + c * 100 + d * 1000 = i Then n = n + 1Next iPrint n 3. 分硬币31. 把一张一元钞票,换成一分、二分和五分硬币,每种至少8枚,问有多少种方案?80n = 0For i = 8 To 100 For j

31、 = 8 To 50 For k = 8 To 20 If (i + 2 * j + 5 * k = 100) Then n = n + 1Next k, j, iPrint n32.50元的整币兑换成5元、2元和1元币值(三种币值均有、缺少一种或两种都计算在内)的方法有多少种。146n = 0For i = 0 To 50 For j = 0 To 25 For k = 0 To 10 If (i + 2 * j + 5 * k = 50) Then n = n + 1Next k, j, iPrint n33.50元的整币兑换成5元、2元和1元币值(要求三种币值均有)的方法有多少种。106

32、n = 0For i = 1 To 50 For j = 1 To 25 For k = 1 To 10 If (i + 2 * j + 5 * k = 50) Then n = n + 1Next k, j, iPrint n34. 马克思曾经做过这样一道趣味数学题:有30个人在一家小饭店里用餐,其中有男人、女人和小孩,每个男人花了3先令,每个女人花了2先令,每个小孩花了1先令,共花去50先令。如果要求男人、女人和小孩都有人参与,试求有多少种方案分配男人、女人和小孩的人数。9n = 0For i = 1 To 50 For j = 1 To 25 k = 30 - i - j If (i +

33、 2 * j + 3 * k = 50) And k 0 Then n = n + 1Next j, iPrint n 4. 勾股、弦数35. A,B,C是三个小于或等于100正整数,当满足1/A2+1/B2=1/C2关系时,称为倒勾股数。求130A+B+CBC的倒勾股数有多少组。1For c = 1 To 100 For b = c + 1 To 100 For a = b + 1 To 100 i = a + b + c If (i 100 And i BC,求A,B,C之和小于100的倒勾股数有多少组? 2For c = 1 To 100 For b = c + 1 To 100 For

34、 a = b + 1 To 100 i = a + b + c If (i 100 And (1 / (a * a) + 1 / (b * b) = 1 / (c * c) Then n = n + 1 Print a, b, c End IfNext a, b, cPrint n37. 勾股弦数是满足公式: A2+B2=C2 (假定ABC)的一组正整数(A,B,C),例如,(3,4,5)是勾股弦数,因为:32+42=52。求A,B均小于25且A+B+C=100的勾股弦数的个数。 11n = 0For c = 1 To 100For a = 1 To 24For b = a + 1 To 24

35、s = a + b + cIf a 2 + b 2 = c 2 And s BC,求A,B,C均小于或等于100的倒勾股数有多少组?5For c = 1 To 100 For b = c + 1 To 100 For a = b + 1 To 100 i = a + b + c If 1 / (a * a) + 1 / (b * b) = 1 / (c * c) Then n = n + 1 Print a, b, c End IfNext a, b, cPrint n39.勾股弦数是满足公式: A2+B2=C2 (假定AB Max Then Max = sNext c, b, aPrint

36、Max40 若某整数平方等于某两个正整数平方之和的正整数称为弦数。例如:由于32+42=52,则5为弦数,求100,200之间弦数的个数。77n = 0For k = 100 To 200For i = 1 To kFor j = i + 1 To kIf i 2 + j 2 = k 2 Then n = n + 1Next j, i, kPrint n41若某正整数平方等于某两个正整数平方之和,称该正整数为弦数。例如:由于32+42=52,则5为弦数,求131,200之间最小的弦数。135For k = 131 To 200For i = 1 To kFor j = i + 1 To kIf

37、 i 2 + j 2 = k 2 Then GoTo 10Next j, i, k10Print k 5.完数因子42 求在10,1000之间的所有完数之和。各真因子之和(不包括自身)等于其本身的正整数称为完数。例如:6=1+2+3,6是完数。524Sum = 0For m = 10 To 1000 s = 0 For i = 1 To m - 1 If m Mod i = 0 Then s = s + i Next iIf s = m Then Sum = Sum + mNext mPrint Sum43 一个数如果恰好等于它的所有真因子之和,这个数就称为“完数”。例如, 6的真因子为1,2

38、,3,而6=1+2+3,因此,6是“完数”。求1,1000之间的最大完数。496Sum = 0For m = 1 To 1000 s = 0 For i = 1 To m - 1 If m Mod i = 0 Then s = s + i Next i If s = m Then Sum = mNext mPrint Sum44 一个数如果恰好等于它的所有真因子之和,这个数就称为“完数”。例如,6的真因子为1,2,3,而6=1+2+3,因此,6是“完数”。求1,1000之间的第二大完数。28Sum = 0For m = 1 To 1000 s = 0 For i = 1 To m - 1 If m Mod i = 0 Then s = s + i Next i If s = m Then n = n + 1 If n = 2 Then Sum = m: Exit ForNext mPrint Sum45 一个数如果恰好等于它的所有真因子之和,这个数就称为“完数”。例如,6的真因子为1,2,3,而6=1+2+3,因此,6是“完数”。求1000以内的所有完数之和。530Sum = 0For m = 1 To 1000 s = 0 For i = 1 To m - 1 If m

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 单元课程

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁