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 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