编写程序查找一个1~10000的armstrong数。若一个N位的正整数,其各位数字的N次方之和等于这个数的本身,则这个数就是armstrong数。例如:153=1^3+5^3+3^3。程序中应定
义一个名为arms()的函数过程,用于判断一个数是否为armstrong数。
刚写的。。不算难啊
Private Function arms(ByVal Num%) As Boolean
If Num >= 1 And Num <= 10000 Then
Dim L%
L = Len(CStr(Num))
Dim T%
Dim i%
For i = 1 To L
T = T + Int(Mid(LTrim(Str(Num)), i, 1)) ^ L
Next
Debug.Print T
arms = IIf(T = Num, True, False)
Else
arms = False
End If
End Function
有问题追问,别忘了给分啊
Private Sub Command1_Click()
Dim i As Integer, s As Integer
For i = 1 To 10000
If armstrong(i) = True Then
Print i;
s = s + 1
End If
If s Mod 5 = 0 Then Print
Next i
End Sub
Private Function armstrong(k As Integer) As Boolean
If Len(Trim(Str(k))) = 1 Then
If k ^ 1 = k Then armstrong = True
ElseIf Len(Trim(Str(k))) = 2 Then
If (k \ 10) ^ 2 + (k Mod 10) ^ 2 = k Then armstrong = True
ElseIf Len(Trim(Str(k))) = 3 Then
If (k \ 100) ^ 3 + ((k Mod 100) \ 10) ^ 3 + ((k Mod 100) Mod 10) ^ 3 = k Then armstrong = True
ElseIf Len(Trim(Str(k))) = 4 Then
If (k \ 1000) ^ 4 + ((k Mod 1000) \ 100) ^ 4 + (((k Mod 1000) Mod 100) \ 10) ^ 4 + (((k Mod 1000) Mod 100) Mod 10) ^ 4 = k Then armstrong = True
ElseIf Len(Trim(Str(k))) = 5 Then
If (k \ 10000) ^ 5 + ((k Mod 10000) \ 1000) ^ 5 + (((k Mod 10000) Mod 1000) \ 100) ^ 5 + ((((k Mod 10000) Mod 1000) Mod 100) \ 10) ^ 5 + ((((k Mod 10000) Mod 1000) Mod 100) Mod 10) ^ 5 = k Then armstrong = True
End If
End Function