(3) \ 、(4) k = k + p * h 17(1) isualVi 、(2) sualVisu 18(1) s + a 、(2) n + 1 、
(3) x As Single, n As Integer 、(4) p * (x + i) / ((2 * i - 1) * i * x)
三、改错题
1、
Option Base 1
Private Sub Command1_Click()
Dim A() As Integer, I As Integer, J As Integer, Logic As Boolean Dim K As Integer ReDim A(1) A(1) = 50
———————— 'K=1 For I = 51 To 149 K = 1 '位置错 Logic = False
Call Sub1(A, I, Logic) If Logic Then K = K + 1
ReDim A(K) 'ReDim Preserve A(K) A(K) = I End If Next I
For I = 1 To UBound(A)
Text1 = Text1 & Str$(A(I))
If I Mod 5 = 0 Then Text1 = Text1 & vbCrLf Next I End Sub
Private Sub Sub1(A() As Integer, N As Integer, F As Boolean) Dim I As Integer, J As Integer, Ub As Integer Ub = UBound(A) For I = 1 To Ub
For J = 2 To A(I)
If A(I) Mod J = 0 And N Mod J = 0 Then Exit For 'Exit Sub End If Next J F = True Next I End Sub
2、
Option Explicit Option Base 1
Private Sub Command1_Click()
Dim I As Integer, Flg As Boolean, S1 As Integer, S2 As Integer Dim J As Integer, K As Integer, P() As Integer ReDim P(1) P(1) = 2
For I = 3 To 150 Step 2 For J = 2 To Sqr(I)
If I Mod J = 0 Then Exit For Next J
If J > Sqr(I) Then
ReDim Preserve P(UBound(P) + 1)
P(UBound(P) + 1) = I 'P(UBound(P)) = I End If Next I
For I = 80 To 125
If fun(I, P, S1, S2) Then 'If fun(I, P, S1, S2)
Text1 = Text1 & Str$(I) & \ End If Next I End Sub
Private Function fun(N As Integer, P() As Integer, S1 As Integer, S2 As Integer) As Boolean Dim I As Integer, J As Integer For I = 1 To UBound(P)
For J = 1 To UBound(P) - 1 If N = P(I) * P(J) Then S1 = P(I) S2 = P(J)
'fun = True Exit Function fun = True '位置错 End If Next J Next I End Function 3、
Option Explicit
Private Sub Get_Chess(Chess() As Integer, idx As Integer, Over As Integer) 'ByVal idx As Integer
Dim Ub As Integer, Counter As Integer, K As Integer Ub = UBound(Chess) Chess(idx) = 0 Counter = 1 K = 0 '位置错 Do Until Counter = Ub - 1 Do Until K = 2 idx = idx + 1
If idx > Ub Then idx = 1 K = K + Chess(idx) Loop
Chess(idx) = 0
Counter = Counter + 1 Loop
For Over = 1 To Ub
If Chess(Over) <> 0 Then Exit For Next Over End Sub
Private Sub Command1_Click()
Dim Chess(32) As Integer, I As Integer, J As Integer Dim K As Integer, idx As Integer For I = 1 To 32
For J = 1 To 32 Chess(J) = 1 Next J
Call Get_Chess(Chess, I, K) If K = 23 Then
Text1 = \从第\号棋子开始取\ Exit For End If Next I End Sub 5、
Option Explicit
Private Sub Form_Click()
Dim S As String, Flg As Boolean Do
S = InputBox(\输入一个自然数\ If S = \
MsgBox \请重新输入\ Else
Exit Do End If Loop
judge(S, Flg) ' Call judge(S, Flg) 或 judge S, Flg If Flg Then
Print S; \是回文数\ Else
Print S; \不是回文数\ End If End Sub
Private Sub judge(Ch As String, F As Boolean) Dim L As Integer, I As Integer L = Len(Ch) F = True ' F = False For I = 1 To L
If Mid$(Ch, I, 1) <> Mid$(Ch, L + 1 - I, 1) Then Exit Sub Next I F = False ' F = True End Sub
四、编程题
1、编程验证任一大于2的偶数可以表示成两个素数之和。 Private Function Prime(a As Integer) As Boolean Dim i As Integer For i = 2 To Sqr(a)
If a Mod i = 0 Then Exit Function Next i
Prime = True End Function
'方法一
Private Sub Command1_Click() Dim m As Integer, i As Integer
m = InputBox(\请输入一个大于2 的偶数\
If m <= 2 Or m Mod 2 <> 0 Then MsgBox \请输入大于2的偶数\ If Prime(2) And Prime(m - 2) Then Print m & \ Else Do
i = i + 1
If Prime(2 * i + 1) And Prime(m - (2 * i + 1)) Then Print m & \
Exit Do End If
Loop While 2 * i + 1 < m - (2 * i + 1) End If End Sub '方法二
Private Sub Command2_Click()
Dim m As Integer, i As Integer, p() As Integer, n As Integer Dim flg As Boolean
m = InputBox(\请输入大于2的偶数\
If m <= 2 Or m Mod 2 <> 0 Then MsgBox \请输入大于2的偶数\ ReDim p(1) p(1) = 2 n = 1
For i = 3 To m - 2 Step 2 If Prime(i) Then n = n + 1
ReDim Preserve p(n) p(n) = i End If Next i
For i = 1 To n For j = 1 To n
If p(i) + p(j) = m Then
Print m & \ flg = True Exit For End If Next j
If flg = True Then Exit For Next i End Sub
2、求组合数
(n>=m),程序中必须包含一个求阶乘的通用过程。
Private Function Fact(a As Integer) As Long If a = 0 Or a = 1 Then Fact = 1 Else
Fact = a * Fact(a - 1) End If
End Function
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库第一章 VB开发环境与VB概述 习题答案(4)在线全文阅读。
相关推荐: