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列由两位正整数组成的数组,并找出其中所有的峰元素。所谓峰元素是指本行内为最大,在本列中也为最大的数组元素。
Option Base 1
Dim a(4, 5) As Integer
Private Sub Command1_Click()
Dim i As Integer, j As Integer Randomize For i = 1 To 4 For j = 1 To 5
a(i, j) = Int(Rnd * 90 + 10) Picture1.Print a(i, j); Next j
Picture1.Print Next i End Sub
Private Sub Fp(a() As Integer, i As Integer, maxj As Integer, Flg As Boolean) Dim max As Integer, j As Integer, t As Integer max = a(i, 1): maxj = 1 For j = 2 To UBound(a, 2) If a(i, j) > max Then max = a(i, j) maxj = j End If Next j
For t = 1 To UBound(a, 1) If a(t, maxj) > max Then Flg = False Exit Sub End If Next
Flg = True End Sub
Private Sub Command2_Click()
Dim i As Integer, m As Integer, mj As Integer, f As Boolean For i = 1 To 4