本程序的功能是:找出所有满足各位数字之和正好是其所有质因子之和的三位数。例如378,其各位数字之和为18,而其质因子2,3,3,3,7之和也是18,所以378是满足条件的数。
Option Explicit
Private Sub CmdFind_Click()
Dim i As Integer,p As String,st As String,st1 As String
For i=100 To 999
()
If csum(i)=zyz(i,st) Then
p=CStr(i)
st1=Left(p,1) & "+" & Mid(p,2,1) & "+" & Right(p,1)
List1.AddItem i & ": " & st1 & "=" & Left(st,Len(st)-1)
End If
Next i
If List1.ListCount=0 Then
List1.AddItem "无满足要求的数!"
End If
End Sub
Private Function csum(m As Integer)As Integer ’求各位数字之和
Dim i As Integer,p As String
p=CStr(m)
For i=1 To Len(p)
()
Next i
End Function
Private Function zyz(ByVal m As Integer,st As String)As Integer ’求所有质因子之和
Dim i As Integer
i=2
Do
If m Mod i=0 Then
zyz=zyz+i
st=st & i & "+"
m=m\i
EIse
()
End If
Loop Until ()
End Function
参考答案:st="";csum=csum+val(mid(p,i,A));i=i+A;m=A