VB实验教材的实验与习题答案(6)

2019-04-13 23:33

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 End If Next n End Sub

8、编写程序,通过调用通用过程找出30以内所有的无平方因子数。若一个数不能被大于1的数的平方整除,则该数就是一个无平方因子数。 Private Sub Command1_Click()

Dim n As Integer, j As Integer, flg As Boolean For n = 2 To 30 flg = False For j = 2 To n

If pfs(j) And n Mod j = 0 Then flg = True Exit For End If Next j

If flg = False Then Print n; Next n End Sub

Private Function pfs(n As Integer) As Boolean If Sqr(n) = Int(Sqr(n)) Then pfs = True End Function

9、编写程序,通过调用通用过程验证下列命题:任意一个正整数和其反序数相加,得到一个新的正整数,再对这个新整数重复上述步骤,最终一定可以得到一个回文数(例如,正整数351,其反序数为153,351+153=504,504+405=909,909是回文数)。 '求反序数

Private Function fxs(n As Long) As Long Dim i As Integer, s As String For i = 1 To Len(CStr(n)) s = Mid(CStr(n), i, 1) & s Next i fxs = s End Function

'判断某数是否为回文数

Private Function Hws(n As Long) As Boolean Dim i As Integer, s As String, L As Integer s = CStr(n): L = Len(s) For i = 1 To Len(s) / 2

If Mid(s, i, 1) <> Mid(s, L + 1 - i, 1) Then Exit Function Next i

Hws = True End Function

Private Sub Command1_Click() Dim x As Long, t As Long

x = InputBox(\请输入一个正整数:\ t = x

Do While Hws(t) = False

Print t; \ t = t + fxs(t) Loop End Sub

10、找出1000以内所有的奇妙平方数,程序中必须包含一个判定某事是否是奇妙平方数的过称。所谓奇妙平方数,是指此数的平方与它的逆序数的平方互为逆序数。例如,122=144,212=441,12与21互逆,144与441互逆,12就是奇妙平方数。 '求反序数

Private Function fxs(n As Long) As Long Dim i As Integer, s As String For i = 1 To Len(CStr(n))

s = Mid(CStr(n), i, 1) & s Next i fxs = s End Function

Private Sub Command1_Click() Dim n As Long, t As Long For n = 1 To 999 t = fxs(n)

If t ^ 2 = fxs(n ^ 2) And n Mod 10 <> 0 And n < t Then List1.AddItem n & \ \

List2.AddItem n & \ \ End If Next n End Sub

11、编写程序,查找介于300~2000之间的所有只有两个数字相同的完全平方数,程序中必须包含一个判定某数是否是只有两个数字相同的完全平方数的过程。所谓完全平方数是指其平方根为整数的数。例如676、900、1521等都是介于300~2000之间符合条件的数。

Private Function Judge(n As Integer) As Boolean

Dim i As Integer, L As Integer, a(9) As Integer, t As Integer If Sqr(n) = Int(Sqr(n)) Then L = Len(CStr(n))

For i = 1 To L

t = Mid(CStr(n), i, 1) a(t) = 1 Next i

For i = 0 To 9

Sum = Sum + a(i) Next i

If Sum = L - 1 Then Judge = True End If End Function

Private Sub Command1_Click() Dim a As Integer For a = 300 To 2000

If Judge(a) Then List1.AddItem a Next a End Sub

12、编写程序,随机生成8个不相等的三位正整数,将他们显示在列表框1中,并在列表框2中显示其中的奇偶间隔数,程序中必须包含一个判定某数是否是就间隔数的过程。所谓奇偶间隔数是指:该数据的各位与百位为奇数,十位为偶数。例如147就是一个就间隔数。 Private Function F(n As Integer) As Boolean Dim i As Integer

For i = 1 To Len(CStr(n))

If i Mod 2 = 1 And Mid(CStr(n), i, 1) Mod 2 <> 1 Then Exit Function If i Mod 2 = 0 And Mid(CStr(n), i, 1) Mod 2 <> 0 Then Exit Function Next i F = True End Function

Private Sub CmdDisp_Click()

Dim a(8) As Integer, i As Integer, j As Integer Randomize Do

t = Int(900 * Rnd + 100) For j = 1 To i

If t = a(j) Then Exit For Next j

If j = i + 1 Then i = i + 1 a(i) = t

List1.AddItem a(i)

If F(a(i)) Then List2.AddItem a(i)

End If Loop Until i = 8 End Sub

Private Sub CmdClear_Click() List1.Clear List2.Clear End Sub

Private Sub CmdEnd_Click() End End Sub

13、编写一个找出所有三位绝对素数的程序。所谓绝对素数是指该数本身是素数,其逆序数也是素数的数。例如,107与701都是素数,所以107和701都是绝对素数。 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 Function nx(n As Integer) As Integer Dim i As Integer, s As String For i = 1 To Len(CStr(n))

s = Mid(CStr(n), i, 1) & s Next i nx = Val(s) End Function

Private Sub CmdFind_Click()

Dim n As Integer, t As Integer For n = 100 To 900 t = nx(n)

If prime(n) And prime(t) And n < t Then List1.AddItem \ End If Next n End Sub

14、随机生成一个4行5列由两位正整数组成的数组,并找出其中所有的峰元素。所谓峰元素是指本行内为最大,在本列中也为最大的数组元素。


VB实验教材的实验与习题答案(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:职业教育现代学徒制试点项目申报书

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

马上注册会员

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