五、
以不同的字母代表
0
—
9
之间的数字,现有如下等式成立:
a+bc+def=ghij
编程求出上述条件等式的个数并将所有等式输出。
答:
源程序:
Private Sub Command1_Click()
Dim i, j, m, n As Integer
Dim l As Integer
Dim s As String
l = 0
Text1.Text = ""
For i = 0 To 9
For j = 10 To 99
For m = 100 To 999
n = i + j + m
s = CStr(i & j & m & n)
If Len(CStr((n))) = 4 And diff(s) Then
Text1.Text = Text1.Text & i & "+" & j & "+" & m & "=" & n & vbCrLf
l = l + 1
End If
Next m
Next j
Next i
Text2.Text = "查找到满足条件的等式个数为:" & l
End Sub
Private Function diff(ByVal s As String) As Boolean
Dim i, j As Integer
Dim a() As Integer
i = Len(s)
diff = True
ReDim a(i)
For j = 1 To i
a(j) = Int(Mid(s, j, 1))
Next j
For j = 1 To i - 1
For k = j + 1 To i
If a(j) = a(k) Then
diff = False
Exit Function
End If
Next k
Next j
End Function
六、
下面的竖式表示乘法运算,其中
“
*
”
只能用素数
2
,
3
,
5
,
7
代替,因此称为素数乘法竖式。
* * *
×
* *
--------------------
* * * *
* * * *
-------------------
* * * * *
答:
源程序:
Option Base 1
Dim a(4) As Integer
Private Sub Command1_Click()
Dim i, j, k As Integer
Dim m As Long, n As Long, p As Long
Dim arr() As Long
Dim brr() As Long
Dim idx1 As Integer
Dim idx2 As Integer
idx1 = 0
idx2 = 0
a(1) = 2
a(2) = 3
a(3) = 5
a(4) = 7
For i = 1 To 4
For j = 1 To 4
idx1 = idx1 + 1
ReDim Preserve brr(idx1)
brr(idx1) = a(i) * 10 + a(j)
For k = 1 To 4
idx2 = idx2 + 1
ReDim Preserve arr(idx2)
arr(idx2) = a(i) * 100 + a(j) * 10 + a(k)
Next k
Next j
Next i
For i = 1 To UBound(arr)
For j = 1 To UBound(brr)
m = arr(i) * (brr(j) \ 10)
n = arr(i) * (brr(j) Mod 10)
p = arr(i) * brr(j)
If pd(m) And pd(n) And pd(p) Then
Text1.Text = arr(i) \ 100
Text2.Text = arr(i) \ 10 Mod 10
Text3.Text = arr(i) Mod 10
Text4.Text = brr(j) \ 10
Text5.Text = brr(j) Mod 10
Text6.Text = n \ 1000
Text7.Text = n \ 100 Mod 10
Text8.Text = n \ 10 Mod 10
Text9.Text = n Mod 10
Text10.Text = m \ 1000
Text11.Text = m \ 100 Mod 10
Text12.Text = m \ 10 Mod 10
Text13.Text = m Mod 10
Text14.Text = p \ 10000
Text15.Text = p \ 1000 Mod 10
Text16.Text = p \ 100 Mod 10
Text17.Text = p \ 10 Mod 10
Text18.Text = p Mod 10
End If
Next j
Next i
End Sub
Private Function pd(ByVal x As Long) As Boolean
Dim b() As Integer
Dim l As Integer
l = 0
ReDim b(Len(CStr(x)))
For i = 1 To Len(CStr(x))
b(i) = Int(Mid(CStr(x), i, 1))
For j = 1 To 4
If b(i) = a(j) Then
l = l + 1
'pd = True
Exit For
End If
Next j
If j > 4 Then pd = False: Exit Function
Next i
If l = Len(CStr(x)) Then
pd = True
Else
pd = False
End If
End Function
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
Text10.Text = ""
Text11.Text = ""
Text12.Text = ""
Text13.Text = ""
Text14.Text = ""
Text15.Text = ""
Text16.Text = ""
Text17.Text = ""
Text18.Text = ""
End Sub
七、
求完全数:
如果一个数等于它的所有约数(除去本身)的和,这个数就称为完全数。试输出
M
和
N
之间完全数。
答:
源程序:
Private Sub Command1_Click()
Dim m, n As Long
Dim i As Integer, j As Integer
m = Val(Text1.Text)
n = Val(Text2.Text)
For i = m To n
If ys(i) Then
Text3.Text = Text3.Text & i & " "
End If
Next i
End Sub
Private Function ys(ByVal x As Integer) As Boolean
Dim i As Integer
Dim s As Integer
s = 0
For i = 1 To x - 1
If x Mod i = 0 Then
s = s + i
End If
Next i
If s = x Then
ys = True
Else
ys = False
End If
End Function
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
八、孪生数:
如果A的约数之和等于B,B的约数之和等于A,A和B称为孪生数。试找出M和N之间的孪生数。
若A为素数,而A+2也是素数,则称A,A+2位一对孪生素数。如:3、5位一对孪生素数。编程求出100以内的孪生素数及共有多少对?
答:
源程序:
Dim a() As Integer
Private Sub Command1_Click()
Dim m, n As Long
Dim s As Integer
Dim i As Integer, j As Integer
m = Val(Text1.Text)
n = Val(Text2.Text)
For i = m To n
s = ys(i)
If i = ys(s) Then
Text3.Text = Text3.Text & i & "="
For j = 1 To UBound(a) - 1
Text3.Text = Text3.Text & a(j) & "+"
Next j
Text3.Text = Text3.Text & a(j) & vbCrLf
End If
Next i
End Sub
Private Function ys(ByVal x As Integer)
Dim i As Integer, idx As Integer
idx = 0
Erase a()
For i = 1 To x - 1
If x Mod i = 0 Then
idx = idx + 1
ReDim Preserve a(idx)
a(idx) = i
ys = ys + i
End If
Next i
End Function
Private Function prime(ByVal y As Integer) As Boolean
Dim i As Integer
For i = 2 To Sqr(y)
If y Mod i = 0 Then prime = False: Exit Function
Next i
prime = True
End Function
Private Sub Command2_Click()
Dim i As Integer
For i = 1 To 100
If prime(i) And prime(i + 2) Then
Text4.Text = Text4.Text & i & "和" & i + 2 & "是孪生兄弟 " & vbCrLf
End If
Next i
End Sub
posted on 2006-09-27 12:09
木子李 阅读(236)
评论(0) 编辑 收藏 引用 网摘 所属分类:
Visual Basic 课堂