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
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
Sum = Sum + a(i) Next i
If Sum = L - 1 Then Judge = True End If
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 f = False m = 0: mj = 0 Call Fp(a, i, mj, f) If f = True Then
Text1.Text = Text1.Text & \ End If Next i End Sub
Private Sub Command3_Click() Picture1.Cls Text1.Text = \End Sub
15、编写程序,随机生成30个无重复数的三位整数,找出其中的降序数。所谓降序数是指所有高位数字都大于低位数字的数。
Private Sub Command1_Click()
Dim a(30) As Integer, i As Integer, j As Integer Dim Flag As Boolean 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
Text1.Text = Text1.Text + Str(a(i))
If i Mod 10 = 0 Then Text1.Text = Text1.Text + vbCrLf If IsDown(a(i)) Then List1.AddItem a(i) Flag = True End If End If
Loop Until i = 30
If Flag = False Then List1.AddItem \无降序数\End Sub
Private Function IsDown(n As Integer) As Boolean Dim i As Integer, S As String S = CStr(n)
For i = 1 To Len(S) - 1
If Mid(S, i, 1) < Mid(S, i + 1, 1) Then Exit Function Next i
IsDown = True End Function