VB编写“水仙花数”

2024-12-29 02:28:46
推荐回答(2个)
回答1:

Private Sub Form_Click()
Dim i%, j%, m%, n%
For n = 100 To 999
i = n \ 100
j = (n Mod 100) \ 10
m = n Mod 10
If i ^ 3 + j ^ 3 + m ^ 3 = n Then
Print n; "="; i; "^3+"; j; "^3+"; m; "^3"
End If
Next n
End Sub

回答2:

代码: Private Sub Command1_Click()
Print "所有水仙花数是:"
Dim i As Integer
For i = 100 To 999
If sxh(i) Then
Print i
End If
Next i
End SubFunction sxh(x As Integer) As Boolean
Dim a As Integer, b As Integer, c As Integer
a = x Mod 10
b = x \ 10 Mod 10
c = x \ 100
If a ^ 3 + b ^ 3 + c ^ 3 = x Then
sxh = True
Else
sxh = False
End If
End Function 图: