VB程序设计多试题及答案

更新时间:2024-04-27 22:02:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

9、Visual Basic程序改错

1. 程序功能:求s=1+3+5+7+...直到s>2000为止。程序中有两行有错误。改正错误,使它能输出正确的结果。 (2025) 平方和

Private Sub Form_Click()

Dim i As Integer, s As Long s = 0 : i = 1

Do Until s < 2000 s = s + i i = i + 1 Loop Print s End Sub

答案:s>2000 ; i+2

2. 程序功能:计算s=2!+4!+8!。程序中有两行有错误。

改正错误,使它能输出正确的结果。 (40346) Private Sub Command1_Click()

Dim k As Integer, i As Integer Dim s As Long, t As Long s = 0 i = 1

Do While i <= 3 t = 1 k = 1

While k <= 2*i t = t * k k = k + 1 Wend s = s + t i = i + 1 wend Print s End Sub

答案:k<=2^i ; loop

3. 程序功能:计算s=2!+4!+6!+8!。程序中有错误。改正错误,使它能输出正确的结果。 (41066) Private Sub Command1_Click()

Dim k As Integer, i As Integer Dim s As Long, t As Long s = 0 : i = 1

Do While i <= 4 t = 1 k = 1

While k <= 2^i

t = t * k k = k + 1 Wend s = s + t i = i + 1 wend Print s End Sub

答案:k<=2*i ; loop

4. 程序功能:求200到400间,能被3整除但不能被7整除的数的个数。程序中有两行有错误。改正错误,使它能输出正确的结果。(57) Private Sub Command1_Click()

Dim count As Integer Dim x As Integer count = 0 x = 200

While x <= 400

If x Mod 3 = 0 or x Mod 7 <> 0 Then count = count + 1 End If Loop

x = x + 1

Print count End Sub

答案: and ; wend

5. 程序功能:求1到400间,同时能被3和7整除的数的个数。程序中有两行有错误。改正错误,使它能输出正确的结果。(19) Private Sub Form_Click()

Dim count As Integer Dim x As Integer For x = 1 To 400

if mod(x,3)=0 and mod(x,7)=0 then count = count + 1 end if Next count Print count End Sub

答案:x Mod 3 = 0 And x Mod 7= 0; x

6. 程序功能:求能被3整除且至少有一位数字为5的三位数的个数。程序中有两行有错误。改正错误,使它能输出正确的结果。(85) Private Sub Command1_Click()

Dim count As Integer

Dim a As Integer, b As Integer, c As Integer Dim x As Integer x = 100

do While x <= 999 If x Mod 3 = 0 Then a = Int(x / 100)

b = Int((x - a * 100) / 10) c = x - a * 100 - b * 10

If a = 5 and b = 5 and c = 5 Then count = count + 1 End If End If x = x + 1 wend Print count End Sub

答案:a = 5 or b = 5 or c = 5 ; loop

7. 程序功能:求三位偶数中,个位数字与十位数字之和除以10所得的余数是百位数字的数的个数。程序中有两行有错误。改正错误,使它能输出正确的结果。(45) 奇数都是45

Private Sub Command1_Click()

Dim count As Integer

Dim a As Integer, b As Integer, c As Integer Dim x As Integer count = 0 x = 100

While x <= 999

a = Int(x / 100)

b = Int(x / 10) - a * 10 c = x - a * 100 - b * 10

If Mod(x,2) = 0 or mod(b + c,10) = a Then count = count + 1 End If x = x + 1 loop

Print count End Sub

答案:x Mod 2 = 0 And (b + c) Mod 10 = a; wend 8. 程序功能:求平方和小于2000的最大自然数。程序中有两行有错误。改正错误,使它能输出正确的结果。(18)

Private Sub Form_Click() Dim x As Integer Dim s As Integer s = 0 x = 0

Do While s >= 2000 x = x + 2 s = s + x * x

1

loop Print x End Sub

答案:s<=2000 ; x = x + 1

9. 程序功能:解百马百瓦古题。大、小马和马驹共100匹,共驮100片瓦。大马一驮三,小马一驮二,马驹二驮一,一次驮完,三种马都驮,共有多少种组合? 程序中有三行有错误。改正错误,使它能输出正确的结果。( 6 )

Private Sub Form_Click()

Dim x As Integer, y As Integer, z As Integer Dim s As Integer s = 0

For x = 1 To 33 For y = 1 To 50 z = 100 - x - y

If 3 * x + 2 * y + z / 2 <>100 Then s = s + 1 End If Next x Next y Print s End Sub

答案:3 * x + 2 * y + z / 2 = 100; Next y; Next x

10. 程序功能:求100-200之间的所有素数个数,程序中有两行有错误。改正错误,使它能输出正确的结果。( 21)

Private Sub Command1_Click() Number = 0

For x = 101 To 199 Step 2 i = 2

k = Int(x/2) Do While i <= k

If x Mod i = 0 Then Exit for i = i + 1 Loop

If i < k Then

Number = Number + 1 End If Next x

Print Number End Sub

答案:exit do ;i>k

11. 程序功能:sum=d+dd+ddd+??+ddd..d(d为1-9的

数字)。例如:3+33+333+3333(此时d=3,n=4)。从键盘上输入d 的值为8,n的值为9,求sum的值。程序中有两行有错误。改正错误,使它能输出正确的结果。 987654312

Private Sub Form_Click()

Dim d As Integer, n As Integer, i As Integer Dim sum As Long sum = 0

d = Val(InputBox(\n = Val(InputBox(\For i = 1 To n

tn = tn + d sum = sun + tn Next i Print sum End Sub

答案:tn = tn * 10 + d;sum=sum+tn

12. 程序功能:输出水仙花的个数(所谓水仙花数是指一

个三位十进制数,该数的各位数字立方之和等于该数本身。例如153是一个水仙花数,因为

1^3+5^3+3^3=153)程序中有两行有错误。改正错误,使它能输出正确的结果。 4 Private Sub Form_Click() Dim number As Integer

Dim n As Integer, i As Integer, j As Integer, k As Integer

For n = 100 To 999 i = n / 100 ‘百位

j = Int(n / 10)-I*10 ‘十位 k = n Mod 10

If n = i * i * i + j * j * j + k * k * k Then number = numbre + 1 End If Next n

Print number End Sub

答案:j = Int((n - i * 100) / 10); number=number+1

13. 程序功能:求数列2/1,3/2,5/3,8/5,13/8,??

的前10项之和。程序中有两行有错误。改正错误,使它能输出正确的结果。 16.47991 Private Sub Form_Click()

Dim i As Integer, t As Integer, n As Integer Dim a As Integer, b As Integer Dim s As Single

a = 2: b = 1: n = 10: s = 0 For i = 1 To n s = s + a / b t =b a = a + b a = t Next i Print s End Sub

答案:t = a ; b = t 14. 程序功能:猴子第1天摘下若干桃子,当即吃掉一半,

又多吃一个,第二天将剩余的部分吃掉一半还多一个;以此类推,到第10天只剩余1个。问第1天共摘了多少桃子。程序中有两行有错误。改正错误,使它能输出正确的结果。 1534 Private Sub Form_Click()

Dim day As Integer, sum As Integer day = 10 ; sum = 1 Do While day > 1 sum = sum * 2 day = day - 1 Loop

Print sum End Sub

答案:day = 10 : sum = 1 ; sum = (sum + 1) * 2

15. 程序功能:求1~200之间的能被7整除的数的平方

和。程序有两处错误。改正错误,使它能输出正确的结果。 377986

Private Sub Command1_Click()

Dim x As Integer Dim sum As Integer x = 1 sum = 0

While x <= 200 If x Mod 7 = 0 sum = sum + x ^ 2 End If x = x + 1 Wend Print sum End Sub

答案:Dim sum As Double ; If x Mod 7 = 0 Then

16. 程序功能:求1~99的平方根的和并输出结果。(保

留小数点两位),程序中有两行有错误。改正错误,使它能输出正确的结果。 661.46 Private Sub Command1_Click()

Dim x As Integer Dim sum As integer x = 1

Do While x <= 99 sum = sum + Sqrt(x) x = x + 1 Loop

Print Round(sum, 2) End Sub

答案:Dim sum As Single; sum = sum + Sqr(x)

2

17. 程序功能:求[1,5000]内能被5整除的前若干个偶数

之和,直到和大于50000为止。程序中有两行有错误。改正错误,使它能输出正确的结果。 50500 Private Sub Command1_Click()

Dim x As Integer Dim s As Long x = 2

Do While x <= 5000 If x Mod 5 = 0 Then s = s + x

If s > 50000 Then Exit for End If End If x = x + 2 Loop Print s End Sub

答案:exit do (只有一处有错)

18. 程序功能:求3000以内能被17或23整除的正整数

的个数。程序中有两行有错误。改正错误,使它能输出正确的结果。 299

Private Sub Command1_Click() Dim x As Integer Dim count As Integer

For x = 1 To 3000 Step -1

If x Mod 17 = 0 and x Mod 23 = 0 Then count = count + 1 End If Next x

Print count End Sub

答案:1;or

19. 程序功能:已知一个数列的前3项为0,0,1,以后

每项为前3项的和,求此数列的第36项。程序中有两行有错误。改正错误,使它能输出正确的结果。 334745777

Private Sub Command1_Click()

Dim a As Long Dim b As Long Dim c As Long Dim d As Long Dim i As Integer a = 0: b = 0: c = 1 For i = 1 To 36 d = a a = b b = c

c = a + b + c Next i Print c End Sub

答案:33;c=a+b+d

20. 程序功能:求数列2,4,8,16,32,?前若干项之和。

当和大于9000时,终止求和并输出结果。程序中有两行有错误。改正错误,使它能输出正确的结果。 16382 Private Sub Command1_Click() Dim s As Integer Dim x As Integer x = 1 Do

x = x+ 2 s = s + x Until s > 9000 Print s End Sub

答案:x=x*2;loop until s>9000

21. 程序功能:已知24有8个正整数因子(即:

1,2,3,4,6,8,12,24),而24正好被其因子个数8整除。问[100,300]之间有多少个这样的数。程序中有两行有错误。改正错误,使它能输出正确的结果。 19 Private Sub Command1_Click()

Dim n1 As Integer, n2 As Integer Dim x As Integer Dim i As Integer n2 = 0

For x = 100 To 300 n1 = 0 For i = 1 To 100 If x Mod i = 0 Then

n1 = n1 + 1 End If Next i

If x Mod n1 = 0 Then n2 = n2 + 1 End If Next x Print n2 End Sub

答:n1 = 0; x

22. 程序功能:我国今年的国民生产总值为45600亿元,

若今后每年以9%的增长率增长,计算多少年后能实现国民生产总值翻一番?程序中有两行有错误。改正错误,使它能输出正确的结果。 9 Private Sub Command1_Click() Dim p As Long, p1 As Long Dim n As Integer n = 0

p1 = 45600 p = p1 Do

p = p * (1 + 9) n = n + 1

Loop Until p < 2 * p1 Print n End Sub

答案:p = p * (1 + 0.09); >=

23. 程序功能:已知24有8个正整数因子

(即:1,2,3,4,6,8,12,24),而24正好被其因子个数8整除。求[100,300]之间能被其因子数目整除的数中最大的数。程序中有两行有错误。改正错误,使它能输出正确的结果。 296

Private Sub Command1_Click()

Dim N As Integer Dim s As Integer Dim i As Integer

For N = 300 To 100 Step -1 s = 0

For i = 1 To N

If N Mod i = 0 Then s = s + 1 End If Next i

If s Mod N = 0 Then Print N Exit For End If Next N End Sub

答案:N mod s=0(只有一处错误)

24. 程序功能:有一个三位数满 足下列条件: (1)三位数

字各不相同; (2)此数等于它的各位数字的立方和。求这种三位数的个数。程序中有两行有错误。改正错误,使它能输出正确的结果。 4 153 370 371 407 Private Sub Command1_Click()

Dim x As Integer,a As Integer,b As Integer Dim c As Integer,n As Integer n = 0 x = 100

Do While x >= 999 a = Int(x / 100)

b = Int(x / 10) - a * 10 c = x - a * 100 - b * 10

3

If a <> c And b <> c And a <> b And x = a ^ 3 + b ^ 3 + c ^ 3 Then

n = n + 1 End If x = x + 1 Loop Print n End Sub

答案:x <= 999;将or全部换成and

25. 程序功能:求[3,500]内所有素数之和。程序中有一

行错误,改正错误,使它能输出正确的结果。 21534 Private Sub Command1_Click() Sum = 0

For x = 3 To 500 flag = True

For i = 2 To x - 1 If x Mod i = 0 Then flag = False Exit For End If Next i

If not flag Then Sum = Sum + x End If Next x Print Sum End Sub 答案:flag

26. 程序功能:求[3-1000]之间最大的五个素数之和。程

序中有两行有错误。改正错误,使它能输出正确的结果。4919

Private Sub Command1_Click() n = 0:Sum = 0 x = 1000 Do

flag = True

For i = 2 To x - 1 If x Mod i = 0 Then flag = False Exit do End If Next i

If flag Then Sum = Sum + x n = n + 1

If n = 5 Then Exit Do End If x = x - 1

Loop While x <= 3 Print Sum End Sub

答案:exit for;x>=3

27. 程序功能:把一张一元钞票,换成一分、二分和五分

硬币,每种至少8枚,求方案数。程序中有两行有错误。改正错误,使它能输出正确的结果。 80 Private Sub Command1_Click()

n = 0

For x = 1 To 100 For y = 8 To 50 For z = 8 To 20

If x + y * 2 + z * 5 = 100 Then n = n + 1 Next z Next y Next x Print n End Sub

答案:8;x + y * 2 + z * 5 = 100

28. 程序功能:求区间[500,2500]中按递增顺序的第25

个素数。程序中有两行有错误。改正错误,使它能输出正确的结果。 659

Private Sub Command1_Click() n = 0

For x = 500 To 2500 flag = False

For i = 2 To x - 1 If x Mod i = 0 Then

flag = True Exit For End If Next i

If flag Then n = n + 1

If n = 25 Then Exit For End If Next x Print x End Sub

答案:not flag (只有一处错误) 29. 程序功能:设某四位数的千位数字与十位数字的和等

于百位数字与个位数字的积,例如,对于四位数:9512,9+1=5*2,求所有这样的四位数之和。程序中有一行有错误。改正错误,使它能输出正确的结果。 1078289 Private Sub Command1_Click() Sum = 0

For x = 1000 To 9999 a = Int(x / 1000)

b = Int(x / 100) - a * 10

c = Int(x / 10) - a * 100 - b * 10 d = x - a * 1000 - b * 100 - c * 10 If a + c = b * d Then

Sum = Sum + x End If Next x Print Sum End Sub

答案:b = Int(x / 100) - a * 10 这样的数:207 9992 30. 程序功能:求 [200,300]之间的有奇数个不同因子的

最大整数。程序中有两行有错误。改正错误,使它能输出正确的结果。 289

Private Sub Command1_Click()

For x = 300 To 200 Step -1 n = 0

For i = 1 To x

If x Mod i = 0 Then

n = n + 1 End If Next i

If n/2 <> 0 Then Print x Exit For End If Next x End Sub

答案:x = 300 To 200 Step –1; n Mod 2 <> 0 31. 程序功能:计算

y=1+2/3+3/5+4/7+?+n/(2*n-1)(n=50), 要求:按四舍五入的方式精确到小数点后第二位。程序中有两行有错误。改正错误,使它能输出正确的结果。 26.47 Private Sub Command1_Click() Dim s As Single Dim n As Integer s = 0

For n = 1 To 50

s = s + n /2 * n-1 Next n

Print round(s) End Sub

答案:s = s + n /(2 * n-1);round(s,2) 32. 程序功能:求当N=20时,

1/(1*2)+1/(2*3)+1/(3*4)+?.+1/(N*(N+1))的值。要求:按四舍五入的方式精确到小数点后第二位。程序中有一行有错误。改正错误,使它能输出正确的结果。 0.95

Private Sub Command1_Click()

4

s = 0: n = 1 Do While n <= 20

s = s + 1 / n * (n + 1) n = n + 1 Loop

Print Format(s, \End Sub

答案:s = s + 1 / (n * (n + 1)) 33. 程序功能:求m=50时,表达式

t=1-1/(2*2)-1/(3*3)-?-1/(m*m)的值。要求:按四舍五入的方式精确到小数点后第四位。程序中有两行有错误。改正错误,使它能输出正确的结果。 0.3749 Private Sub Command1_Click()

Dim t As Single Dim m As Integer m = 2 t = 1

Do While m <= 50 t = t - 1 / m * m m = m + 1 wend

Print Format(t, \End Sub

答案:(m * m);loop

34. 程序功能:求[10,1000]之间满足除以7余5、除以5

余3、除以3余1的所有整数的个数。程序中有两行有错误。改正错误,使它能输出正确的结果。 9 Private Sub Command1_Click()

Dim x As Integer Dim n As Integer n = 0

For x = 10 To 1000

If x Mod 7 = 5 or x Mod 5 = 3 or x Mod 3 = 1 Then n = n + 1 End If loop Print n End Sub

答案:将所有的or改成and; next x

35. 程序功能:求1到5000之间的能被5整除的前若干

个偶数之和,直到和大于500为止。程序中有两行错误。改正错误,使它能输出正确的结果。550 Private Sub Command1_Click()

n = 0 : s = 0

For x = 2 To 5000 step 2 If x mod 5 = 0 Then s = s + x End If

If s > 500 Then Exit For Next x Print s End Sub

36. 程序功能:百钱百鸡问题。用100钱买100只鸡,公

鸡一只五钱,母鸡一只三钱,雏鸡三只一钱,编程计算共有几种买法(要求每种鸡至少要买1只)。程序中有一行有错误。改正错误,使它能输出正确的结果。 3 Private Sub Command1_Click() s = 0 cock = 1

Do While cock <= 100 / 5 hen = 1

Do While hen <= 100 / 3 - cock chick = 100 - cock - hen

If cock + hen + chick = 100 Then s = s + 1 End If

hen = hen + 1 Loop

cock = cock + 1 Loop Print s End Sub

答案:cock * 5 + hen * 3 + chick / 3 = 100(一处错) 37. 程序功能:求500以内最大的10个能被13或17整

除的自然数之和。程序中有两行有错误。改正错误,使它能输出正确的结果。 4622 Private Sub Command1_Click() s = 0: n = 0 x = 500

Do While x >= 1

If x Mod 13 = 0 and x Mod 17 = 0 Then s = s + x n = n + 1 End If

If n = 10 Then Exit Do x = x + 1 Loop Print s End Sub

答案:Or;x = x - 1

38. 程序功能:求 [10,1000]之间的所有完数之和。各

真因子之和(不包括自身)等于其本身的正整数称为完数。例如:6=1+2+3,6是完数。程序中有有错误。改正错误,使它能输出正确的结果。524 Private Sub Command1_Click() Dim sum As Integer

Dim x As Integer, k As Integer Dim i As Integer sum = 0

For x = 10 To 1000 k = x

For i = 1 To x-1 If x Mod i = 0 Then K = K - i End If Next i

If k = 0 Then sum = sum + x End If Next x Print sum End Sub

答案: x-1;k=k-i

39. 程序功能:求满足以下条件的(a,b,c)的组数:

(1) 1/(a^2)+1/(b^2)=1/(c^2) ;(2)a>b>c ;(3)a+b+c<100。程序有错误,改正错误,使程序能输出正确的结果。 (2)

Private Sub Command1_Click() s = 0

For c = 1 To 100 For b = 1 To 100 For a = 1 To 100

If a + b + c < 100 or 1 / (a ^ 2) + 1 / (b ^ 2) = 1 / (c ^ 2) Then s = s + 1

Next a Next b Next c Print s End Sub

答案:If a + b + c < 100 And 1 / (a ^ 2) + 1 / (b ^ 2) = 1 / (c ^ 2) And a > b And b > c Then s = s + 1

40. 程序功能:求四位奇数中,各位数字之积(积不为0)

是60的倍数的数之和。程序中有一行错误,改正错误,使程序能输出正确的结果。 3456254 Private Sub Command1_Click() num1 = 0

For x = 1001 To 9999 Step 2 a = Int(x / 1000)

b = Int(x / 100) - a * 10

c = Int(x / 10) - a * 100 - b * 10 d = x - a * 1000 - b * 100 - c * 10 If a * b * c * d Mod 60 = 0 Then num1 = num1 + x End If

5

Next x Print num1 End Sub 答案: a * b * c * d Mod 60 = 0 And b <> 0 And c <> 0 And d <> 0 41. 程序功能:有一堆零件(零件个数不超过1000),如果分成4个零件一组余2个;7个一组余3个; 9个一组余5个。求这堆零件的个数有几种可能。程序中有错误,改正错误,使程序能输出正确的结果。 4 Private Sub Command1_Click() For x = 1 To 1000 If x Mod 4 = 2 or x Mod 7 = 3 or x Mod 9 = 5 Then i = i + 1 End If Next x Print i 答:将or变成 and End Sub 42. 程序功能:百钱百鸡问题。用100钱买100只鸡,公鸡一只五钱,母鸡一只三钱,雏鸡三只一钱,编程计算共有几种买法(要求每种鸡至少要买1只)。程序中有一行有错误。改正错误,使它能输出正确的结果。 3 Private Sub Command1_Click() s = 0 cock = 1 Do While cock <= 100 / 5 hen = 1 Do While hen <= 100 / 3 - cock chick = 100 - cock - hen If cock + hen + chick = 100 Then s = s + 1 End If hen = hen + 1 Loop cock = cock + 1 Loop Print s End Sub 答案:cock * 5 + hen * 3 + chick / 3 = 100 43. 程序功能:求共有几组i、j、k符合算式ijk+kji=1534,其中i、j、k是0~9之间的一位整数。程序中有一行有错误。改正错误,使它能输出正确的结果。 5 Private Sub Command1_Click() n=0 For i =0 To 9 For j=0 to 9 For k=0 to 9 If ijk+kji=1534 Then n=n+1 End If Next k Next j Next i Print n End Sub 答案:i*100+j*10+k+k*100+j*10+i=1534 44. 程序功能:求所有符合算式ij*ji=1300的最小数ij。其中i、j是1~9之间的一位整数。程序中有三行错误。改正错误,使它能输出正确的结果。 25 Private Sub Command1_Click() For i = 1 To 9 For j = 1 To 9 If ij*ji = 1300 Then Print 10 * i + j Exit Sub End If Next i Next j End Sub 答案:(i * 10 + j) * (j * 10 + i);j;i

45. 程序功能:求[444,666]中最大的素数。程序中有两行有错误。改正错误,使它能输出正确的结果。661 Private Sub Command1_Click()

Dim x As Integer Dim i As Integer For x = 666 To 444 k = Int(Sqr(x))

For i = 2 To k If x Mod i = 0 Then Exit do End If Next i If i > k Then Print x Exit For End If Next x End Sub 答案:For x = 666 To 444 Step -1;exit for

46. 程序功能:将50元兑换成5元、2元和1元的方法的种数(每种面额至少2张)。程序中有两行有错误。改正错误,使它能输出正确的结果。 72 Private Sub Command1_Click() For x = 2 To 10 For y = 2 To 25 For z = 2 To 50 If x + y + z = 50 Then N = N + 1 Exit For End If Next z Next y Next x Print N End Sub 答案:5 * x + 2 * y + z = 50;去掉exit for 47. 程序功能:求这样的一个三位数,其个位数不大于2。若将个位数移动到百位之前(如:321移成132),新三位数大于原三位数的两倍。程序中有两行错误,改正错误,使程序能输出正确的结果。 102

Private Sub Command1_Click() For i = 0 To 9 For j = 0 To 9 For k = 0 To 2

N = 100 * i + 10 * j + k m = 100 * k + 10 * i + j If m >= 2 * N Then Print N Next k Next j Next i End Sub 答案:For i = 1 To 9; For k = 1 To 2 48. 编程求方程5X+4Y=2,在|X|<=50,|Y|<=100内的整数

解,X+Y最大值是多少? 112 (填空题)

Private Sub Form_click() Max = 0 For x = -50 To 50 For y = -100 To 100

If 5 * x - 4 * y = 2 Then s = x + y If s > Max Then Max = s Next y Next x Print Max End Sub 49. 求方程5X-4Y=2,在|X|<=100,|Y|<=50内的整数解中|X|+|Y|最大值? 86 (填空题) Private Sub Form_click() Max = 0 For x = -100 To 100 For y = -50 To 50 If 5 * x - 4 * y = 2 Then 6

s = Abs(x) + Abs(y) If s > Max Then Max = s End If Next y Next x Print Max End Sub

50. 找出1-1000之间的全部同构数的和。同构数是指一

个数,它出现在它的平方数的右端。例如,5的平方是25,5是25中右端的数,5就是一个同构数。同理,25也是一个同构数。 1114

Private Sub Command1_Click()

s = 0

For x = 1 To 1000

y = Len(Trim(Str(x))) i = x * x Select y Case 1

If x = Val(Right(Trim(Str(i)), 1)) Then s = s + x End If Case 2

If x = Val(Right(Trim(Str(i)), 2)) Then s = s + x End If Case 3

If x = Val(Right(Trim(Str(i)), 3)) Then s = s + x End If Case 4

If x = Val(Right(Trim(Str(i)), 4)) Then s = s + x End If End Select Next x Print s End Sub

答案:Select Case y

51. 程序功能:求字符串\中数,字

母字符的ASCII码之和。程序中有一行错误,改正错误,使程序能输出正确的结果。1113 Private Sub Command1_Click() alpha = 0

st = \For i = 1 To st

ch = Mid$(st, i, 1) Select Case ch Case \

alpha = alpha + Asc(ch) Case \

alpha = alpha + Asc(ch) End Select Next i

Print alpha End Sub

答案:Len(st)

52. 求字符串“This is my Basic”所有字符的ASCII码

之和。程序中有一行有错误。改正错误,使程序能输出正确结果。1436

Private Sub Command1_Click() Sum = 0

s = \For i = 1 To s

Sum = Sum + Asc(Mid$(s, i, 1)) Next i Print Sum End Sub

答案:Len(s)

53. 求在 1,2,3,...,100中, 任选两个不同的数,要求它

们的和能被3和7整除的数的对数(注意:3+5和5+3认为是同一对数)。 2121

Private Sub Form_Click()

Dim n As Integer, i As Integer n = 0 i = 1

Do While i <= 100 j = 1

Do While j <= 100

If mod((i+j,3)=0 or mod((i+j,7)=0 Then n = n + 1 End If j = j + 1 Loop

i = i - 1 Loop

Print n / 2 End Sub

答案:If ((i + j) Mod 3 = 0 Or (i + j) Mod 7 = 0) And i <> j Then;i=i+1 54. 一个两位数的正整数,如果将其个位数与十位数字对

调所生成的数称为其对调数,如28是82的对调数。现给定一个两位的正整数46,请找到另一个两位的整数,使这两个数之和等于它们的各自的对调数之和。这样的另一个两位数有多少个。程序中有两行有错误。改正错误,使它能输出正确的结果。8 Private Sub Form_Click()

Dim x As Integer, y As Integer, number As Integer Dim xf As Integer, yf As Integer number = 0 x = 46

xf = (x Mod 10) * 10 + Int(y/ 10) For y = 10 To 99

yf = (y Mod 10) * 10 + Int(y / 10) If x + y = xf + yf Then number = number + 1 End If Next y

Print number End Sub

答案:x(只有一处错误) 55. 程序功能:若某整数N的所有因子之和等于N的倍数,

则N称为多因子完备数,如数28,其因子之和

1+2+4+7+14+28=56=2*28,28是多因子完备数。求[1,200]之间有多少个多因子完备数。改正错误,使它能输出正确的结果。 4 Private Sub Command1_Click() Dim N As Integer Dim s As Integer Dim i As Integer Dim c As Integer c = 0

For N = 1 To 200 s = 0

For i = 1 To N

If i Mod N <> 0 Then s = s + i End If Next i

If N Mod s = 0 Then c = c + 1 End If Next N Print c End Sub

答案:i Mod N = 0; s Mod N = 0

56. 若一个四位正整数是另一个正整数的平方,且各位数

字的和是一个平方数,则称该四位正整数是“四位双平方数”。例如: 由于7396=86^2,且

7+3+9+6=25=5^2,则称7396是“四位双平方数”。求最小的 “四位双平方数”。程序中有行一有错误。改正错误,使它能输出正确的结果。1521 Private Sub Command1_Click() For x = 1000 To 9999

7

a = Int(x / 1000)

b = Int(x / 100) - a * 10

c = Int(x / 10) - a * 100 - b * 10 d = x - a * 1000 - b * 100 - c * 10

If Sqr(x) = Int(Sqr(x)) And Int(Sqr(a + b + c + d)) = Sqr(a + b + c + d) Then Print x Exit For End If Next x End Sub

答案:Int(Sqr(a + b + c + d)) = Sqr(a + b + c + d) 57. 程序功能:求10的阶乘,程序中有一行有错误。改

正错误,使它能输出正确的结果。3628800 Private Sub Command1_Click() Print fun(10) End Sub

Private Function fun(n As Integer) As Long If n > 0 Then fun = 1

Else: fun = n * fun(n - 1) End If

End Function 答案:n=0

58. 程序功能:求整数2310的所有质因子(即所有为素

数的因子)之和。程序的Isprime()函数中有一行有错误。改正错误,使它能输出正确的结果。28 Private Sub Command1_Click() s = 0 k = 2310

For j = 2 To k

If (k Mod j = 0 And Isprime(j)) Then s = s + j End If Next j Print s End Sub

Private Function Isprime(n) Dim flag As Boolean flag = True If n = 2 Then Isprime = True Else

For i = 2 To n - 1 If n Mod i = 0 Then flag = False Exit For End If Next i Isprime = true End If

End Function 答案:flag

59. 计算两个数的最大公约数。程序中有一行有错误,改

正错误,使程序能输出正确结果。27 Private Sub Command1_Click() Print fun(1107, 2889) End Sub

Private Function fun(u, v) As Integer While (v <> 0) t = u Mod v u = v v = t Wend fun = v End Function

答案:t;同时将现有的t = u Mod v分移到while之前和v=t之后。(不只一个错误)

60. 程序功能: 建立一个过程来计算12的阶乘,程序中

有错误,改正错误,使程序能输出正确的结果。479001600

Private Sub Command1_Click() Call n(12) End Sub

Public Sub n(a As Integer) Dim i As Integer Dim f As integer f = 0

For i = 1 To a f = f * i Next i Print f End Sub

答案:long;f=1

61. 程序功能:计算两个数的最小公倍数。程序中有两行

有错误,改正错误,使程序能输出正确结果。26288 Private Sub Command1_Click() Print fun(248, 848) End Sub

Private Function fun(u, v) t = u

Do While t Mod u <> 0 Or t Mod v <> 0 t = t + u Loop fun =u

End Function 答案:fun=t

8

10、Visual Basic编程题

1、 已知数列前3项为0、0、1,以后每项为前3项的和,求此数列的第36项。(334745777) 方法一:

方法二:

Private Sub Form_Click() Private Sub Form_Click() Dim a&, b&, c&, d&, n% Dim d(36) As Double, n As Integer a = 0: b = 0: c = 1 d(1) = 0 For n = 1 To 33 Step 1 d(2) = 0 d = a d(3) = 1 a = b For n = 4 To 36

b = c d(n) = d(n - 1) + d(n - 2) + d(n - 3) c =a+b+d Next n Next n Print d(36) Print d End Sub

End Sub

2、 计算y=1+2/3+3/5+4/7+?+n/(2*n-1) (n=50), 要求:按四舍五入的方式精确到小数点后第二位。 (26.47) Private Sub Form_Click() Dim s As Single, n as integer s = 0

For n = 1 To 50 s = s + n / (2 * n - 1) Next n

Print Round(s, 2) End Sub

3、 求数列2/1,3/2,5/3,8/5,??,的前10项的和。(16.47991) 方法一:

方法二:

private sub form_click() Private Sub Form_Click()

dim I%,a%,b%,t%,s! Dim d(10) As Single, n As Integer, s As Single a=2:b=1

d(1) = 2 / 1 for I=1 to 10 step 1 s = 2 / 1 s=s+a/b For n = 2 To 10 t=a d(n) = 1 + 1 / d(n - 1) a=a+b s = s + d(n) b=t Next n next I Print s print s End Sub end sub

4、 编程求fibonacci数列:1,1,2,3,5,8……的前30个数的和。(2178308) 方法一:

print s Private Sub Form_Click() End Sub Dim a&, b&, c&, s& 方法二:

a = 1:b = 1:s = 2 Private Sub Form_Click()

For i = 3 To 30 Dim d(30) As Double, n As Integer, s As Double c = a + b d(1) = 1 s = s + c d(2) = 1 a = b s = 2

b = c For n = 3 To 30 next i d(n)=d(n-1)+d(n-2)

9

Next i Print s End sub

Private Sub Form_Click() Dim a%, b% For a = 0 To 6 For b = 0 To 3

Next n Print s End sub

5、 编写程序,求一正整数等差数列的前五项的立方和,该数列前四项之和是26、之积是880。 (4720)

If a + (a + b) + (a + 2 * b) + (a + 3 * b) = 26 And a * (a + b) * (a + 2 * b) * (a + 3 * b) = 880 Then Print a ^ 3 + (a + b) ^ 3 + (a + 2 * b) ^ 3 + (a + 3 * b) ^ 3 + (a + 4 * b) ^ 3 End If Next b Next a End Sub

6、 猴子吃桃问题:猴子第1天摘下若干桃子,当即吃掉一半,又多吃一个,第二天将剩余的部分吃掉一半还多一个;以此类推,

到第10天只剩余1个。问第1天共摘了多少桃子。 (1534) 方法一:

Private Sub Form_Click() Dim day As Integer, sum As Integer day = 10: sum = 1 Do While day > 1 sum = (sum + 1) * 2 day = day - 1 Loop Print sum End Sub

7、 我国今年的国民生产总值为45600亿元,若今后每年以9%的增长率增长,计算多少年后能实现国民生产总值翻一番?(9) Private Sub Command1_Click() Dim s&, n% n=0 s = 45600

Do While s < 45600 * 2 s = s * 1.09 n = n + 1 Loop Print n End Sub

1、 编写程序,编写程序,求在10~1000之间所有能被4除余3,被7除余4,被9除余4的数的平方和。 (1109620) Private Sub Form_Click() Dim s#, n% s=0

For n = 10 To 1000

If n Mod 4 = 3 And n Mod 7 = 4 And n Mod 9 = 4 Then s = s + n ^ 2 End If Next n Print s

10

方法二:

Private Sub Form_Click() Dim day As Integer, sum As Integer day = 10: sum = 1 For day = 9 To 1 Step -1 sum = (sum + 1) * 2 Next day Print sum End Sub

End Sub

2、 编写程序,统计200~300之间的所有满足三个数字之积为24,三个数字之和为10的数的个数。(2) private sub form_click() dim I%,a%,b%,c%,n% n=0

for I =200 to 300 step 1

a=Int(i/100) ‘百位 b=int((I-a*100)/10) ‘十位 c=I mod 10 ‘个位 if a*b*c=24 and a+b+c=10 then n=n+1 end if next I print n end sub

3、 编写程序,计算1000以内有多少个这样的数,其十位数为6且能被8整除。(15) Private Sub Form_Click() Dim n%,i%,a%,b% n = 0

For i = 1 To 1000 a = i \\ 100 b = (i - a * 100) \\ 10

If b = 6 And i Mod 8 = 0 Then n = n + 1 End If Next i Print n End Sub

4、 \水仙花数\是指这样的数,其各位数字的立方和等于该数本身。编写程序求100至999的范围内有多少个水仙花数。(4) Private Sub Form_Click() Dim i%, n%, a%, b%, c% n = 0

For i = 100 To 999 a = i \\ 100 b = (i - a * 100) \\ 10 c = i Mod 10

If i = a ^ 3 + b ^ 3 + c ^ 3 Then n = n + 1 End If Next i Print n End Sub

5、 编写程序,求满足下列条件的所有四位数ABCD的个数,该四位数是8的倍数,且A+B=B+C,即第1位数加上第2位数等

于第2位数加上第3位数。(110) Private Sub Form_Click() Dim i%, n%, a%, b%, c% n = 0

For i = 1000 To 9999

11

方法二:

a = i \\ 100 '百位 b = (i mod 100) \\ 10 '十位 c = i Mod 10 '个位

a = i \\ 1000

b = (i - a * 1000) \\ 100 c = (i - a * 1000 - b * 100) \\ 10 If i Mod 8 = 0 And a + b = b + c Then n = n + 1 End If Next i Print n End Sub

6、 所谓回文数是从左至右或从右至左读起来都是一样的数字,如:121是一个回文数。编写程序,计算从1981年开始到3000年

为止,共有多少个年号是回文数年号。(11) Private Sub Form_Click() Dim I%, a%, b%, c%, d%, n% For I = 1981 To 3000 Step 1 a = Int(I / 1000)

b = Int((I - a * 1000) / 100) c = Int((I - a * 1000 - b * 100) / 10) d = I Mod 10

If a = d And b = c Then n = n + 1 End If Next I Print n

End Sub 五位数回文数必为这样的格式:abcba。因为回文数是偶数,所以a为偶数,并且a在最高位,不能为零,因此a有4种选择:2、4、6、8,b有10种选择:1、2、3、4、5、6、7、8、9、0,c有10种选择:1、2、3、4、5、6、7、8、9、0。4×10×10=400(个) 求

7、 计算在0至99的范围内有多少个数,其每位数的乘积大于每位数的和。(63) Private Sub Form_Click() Dim i%, n%, a%, b% n = 0 For i = 0 To 99 a = i \\ 10 b = i Mod 10 If a * b > a + b Then n = n + 1 End If Next i Print n End Sub

8、 有一个三位数满足下列条件: (1)三位数字各不相同; (2)此数等于它的各位数字的立方和。 Private Sub Form_Click() Dim i%, n%, a%, b%, c% n = 0

For i = 100 To 999 a = i \\ 100 b = (i - a * 100) \\ 10 c = i Mod 10

12

10000~99999之间的奇数回文数的个数? 500

If a <> b And b <> c and a<>c And a ^ 3 + b ^ 3 + c ^ 3 = i Then n = n + 1 End If Next i Print n End Sub

1、 求方程8x-5y=3在|x|<=150,|y|<=200内的 (1)整数解的组数;(50) (2)|x|*|y|的最大值;(24676) Private Sub Form_Click() Dim x%, y%, n%, max& n = 0: max = 1 For x = -150 To 150 For y = -200 To 200 If 8 * x - 5 * y = 3 Then n = n + 1

If Abs(x) * Abs(y) > max Then max = Abs(x) * Abs(y) End If End If Next y Next x Print n, max End Sub

2、 把50元钱分成一元、二元和五元的纸币且纸币张数共为20张的分法有多少种?(注:在兑换中,一元、二元、五元的纸币

张数可以为0) 4 Private Sub Form_Click() Dim i%, j%, k%, n% n = 0 For i = 0 To 20 For j = 0 To 20 For k = 0 To 20

If 1 * i + 2 * j + 5 * k = 50 And i + j + k = 20 Then n = n + 1 End If Next k Next j Next i Print n End Sub

3、 百钱百鸡问题。用100钱买100只鸡,公鸡一只五钱,母鸡一只三钱,雏鸡三只一钱,编程计算共有几种买法(要求每种鸡至

少要买1只)。(3) Private Sub Form_Click() Dim i%, j%, k%, n% n = 0

For i = 1 To 100 For j = 1 To 100

13

For k = 1 To 100

If 5 * i + 3 * j + k / 3 = 100 And i + j + k = 100 Then n = n + 1 End If Next k Next j Next i Print n End Sub

4、 求共有几组i、j、k符合算式ijk+kji=1534,其中i、j、k是0~9之间的一位整数。(5) private sub form_click() Dim i%, j%, k%, n% n = 0 for i = 0 to 9 for j = 0 to 9

for k = 0 to 9

if i * 100 + j * 10 + k + k * 100 + j * 10 + i = 1534 then

n = n + 1

end if next k next j next i print n end sub

5、 有30个学生一起买小吃,共花钱50元,其中每个大学生花3元,每个中学生花2元,每个小学生花1元,问大、中、小学

生的人数分配共有多少种不同的解(去掉某类学生数为0的解)?(9) Private Sub Form_Click() Dim i%, j%, k%, n% n = 0 For i = 1 To 30 For j = 1 To 30 For k = 1 To 30

If 3 * i + 2 * j + k = 50 And i + j + k = 30 Then n = n + 1 End If Next k Next j Next i Print n End Sub

6、 求方程9x+4y=1000的正整数解的组数以及|x-y|的最大值。(27,237) Private Sub Form_Click() Dim x%, y%, n%, max& n = 0: Max = 0 For x = 1 To 111 For y = 1 To 250

If 9 * x + 4 * y = 1000 Then n = n + 1

14

If Abs(x - y) > Max Then Max = Abs(x - y) End If End If Next y Next x Print n, Max End Sub

7、 有5羊4犬3鸡2兔值钱1496,4羊2犬6鸡3兔值钱1175,3羊1犬7鸡5兔值钱958,2羊3犬5鸡1兔值钱861,求鸡值多少钱? (23)

题型四:也是求某范围内满足条件的数的和,但不能遍历给定范围的所有数,因为题目要求在满足另一条件时强制结束循环。 1、 求1到5000之间的能被5整除的前若干个偶数之和,直到和大于500为止。(550) 方法一:

Private Sub Form_Click() Dim s%, x% s = 0

For x = 2 To 5000 Step 2 If x Mod 5 = 0 Then s = s + x End If

If s > 500 Then

Exit For

End if Next x Print s End Sub

2、 已知:S=1+3+5+7+9+?,求S不大于750的最大值。(729) 方法一:

Private Sub Form_Click() Dim s%, x% s = 1 x = 1

Do While s <= 750 x = 2 + x s = s + x Loop s = s - x Print s End Sub

方法二:

Private Sub Form_Click() Dim s%, x% s = 0 x = 1

Do While 1 '1恒为true s = s + x If s > 750 Then

s = s - x 's此时已经>750,必须减去刚加上去的x Exit Do '跳出do while?loop循环 End If x = x + 2 Loop Print s End Sub 方法二:

Private Sub Form_Click() Dim s%, x% s = 0 x = 1

Do While s <= 500

If x Mod 5 = 0 And x Mod 2 = 0 And x <= 5000 Then s = s + x End If x = x + 1 Loop Print s End Sub

S=1+(1+3)+(1+3+5)+……+(1+3+5+7+……+99) 求S>20000的最小值 S是前N项平方和,由公式可得,n取39时即为所求。答案20540

3、 已知:S=2+4+8+16+32+?,求S不大于1500的最大值。(1022)

15

本文来源:https://www.bwwdw.com/article/opcg.html

Top