vb程序设计实验教程与习题选解 王杰华 郑国平 主编(5)

2019-04-10 10:17

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、求组合数Cn?mn!(n>=m),程序中必须包含一个求阶乘的通用过程。 m!(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

Private Sub Command1_Click()

Dim n As Integer, m As Integer, result As Single n = Val(Text1.Text) m = Val(Text2.Text) If n >= m Then

result = Fact(n) / Fact(m) / Fact(n - m) Text3.Text = result Else

MsgBox \要求n>=m,请重新输入\ Text1.Text = \ Text2.Text = \ Text1.SetFocus End If End Sub

Private Sub Command2_Click() Text1.Text = \ Text2.Text = \ Text3.Text = \ Text1.SetFocus End Sub 3、编写一个查找10到300之间所有同构数的程序,程序中必须包含一个判定某数是否是同构数的Function过程Istgs。若一个数出现在自己平方数的右端,则此数为同构数。如5在52=25的右端,25在252=625的右端,故5和25为同构数。

Private Function Istgs(n As Integer) As Boolean Dim s As String, L As Integer L = Len(CStr(n)) s = CStr(n ^ 2)

If Right(s, L) = n Then Istgs = True End Function

Private Sub Command1_Click() Dim n As Integer For n = 10 To 300

If Istgs(n) Then List1.AddItem n & \\ Next n End Sub

Private Sub Command2_Click() End End Sub

4、编写程序,随机生成一个由三位正整数组成的3行4列的数组,求数组每一行的最大元素及其所在列号,程序中必须包含一个求一行中最大元素及其所在列号的通用过程。(若一行中有多个最大元素则取其中之一即可)

Private Sub CmdDisp_Click()

Dim a(3, 4) As Integer, i As Integer, j As Integer Dim m As Integer, mj As Integer Randomize For i = 1 To 3 For j = 1 To 4

a(i, j) = Int(900 * Rnd + 100) Print a(i, j); Next j Print Next i

For i = 1 To 3 m = 0

Call max(a, i, m, mj)

Print \第\行最大值为\在第\列\ Next i End Sub

Private Sub max(a() As Integer, i As Integer, m As Integer, maxj As Integer) Dim j As Integer m = a(i, 1): maxj = 1

For j = 2 To UBound(a, 2) If a(i, j) > m Then m = a(i, j) maxj = j End If Next j End Sub

Private Sub CmdEnd_Click() End End Sub

5、一个整数N的因子和(不包括N)等于N,则N被成为完全数。例如,28=1+2+4+7+14,28是完全数。编写程序验证:介于10到10000之间的完全数,把它们的各位数字加起来得到一个数,再把这个数的各位数字加起来又得到一个数,一直做下去,直到得到一个一位数,这个数是1,程序中必须包含一个判定某数是否是完全数的过程。例如,28的各位数字加起

来得到一个数10,再把10这个数的各位数字加起来又得到一个数是1。

Private Function IsWqs(n As Integer) As Boolean Dim sum As Integer, i As Integer For i = 1 To n / 2

If n Mod i = 0 Then sum = sum + i Next i

If sum = n Then IsWqs = True End Function

Private Sub Command1_Click() Dim n As Integer Dim sum As Integer For n = 10 To 10000 If IsWqs(n) Then t = n

Print t; \ Do

sum = 0

For i = 1 To Len(t) - 1

sum = sum + Mid(CStr(t), i, 1) Print Mid(CStr(t), i, 1); \ Next i

sum = sum + Mid(CStr(t), i, 1) If sum <> 1 Then

Print Mid(CStr(t), i, 1); \ Else

Print Mid(CStr(t), i, 1); \ End If t = sum

Loop Until sum = 1 Print End If Next n End Sub

Private Sub Command2_Click()

End End Sub

6、找出500以内的超完全数,程序中必须包含一个判定某数是否是超完全数的过程。设符号Ф(N)表示N的所有因子的和(包括N在内),若Ф(Ф(N))=2N,则N就是一个超完全数。例如,16的因子和为1+2+4+8+16=31,而31的因子为1+31=32,32=2*16,故16是一个超完全数。

Private Function Judge(n As Integer) As Boolean Dim sum As Integer sum = yz(n)

If yz(sum) = 2 * n Then Judge = True End Function

Private Function yz(n As Integer) As Integer Dim i As Integer For i = 1 To n

If n Mod i = 0 Then yz = yz + i Next i End Function

Private Sub Command1_Click() Dim n As Integer For n = 1 To 500

If Judge(n) Then Print n Next n End Sub

7、编写程序,通过调用通用过程找出100以内的所有不可表示成两个平方数之和的数。 Private Function pfh(n As Integer) As Boolean Dim i As Integer, t As Single For i = 1 To Int(Sqr(n)) t = n - i ^ 2

If Sqr(t) = Int(Sqr(t)) Then pfh = True Next i End Function

Private Sub Command1_Click() Dim n As Integer, t As Integer For n = 1 To 100

If pfh(n) = False Then t = t + 1 Print n;

If t Mod 10 = 0 Then Print


vb程序设计实验教程与习题选解 王杰华 郑国平 主编(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2013年年度管理方案书

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: