编程求100—999之间的水仙花数;如水仙花数153=1*1*1+5*5*5+3*3*3;

2024-11-25 14:49:48
推荐回答(1个)
回答1:

Private Sub Form_Load()
Dim a As Integer, b As Integer, c As Integer, d As Integer

Print "水仙花数为:";
For a = 0 To 9
For b = 0 To 9
For c = 0 To 9
For d = 0 To 9
If a ^ 3 + b ^ 3 + c ^ 3 + d ^ 3 = a * 1000 + b * 100 + c * 10 + d Then
Print a * 1000 + b * 100 + c * 10 + d;
End If
Next d
Next c
Next b
Next a

Rem 第二种方法求水仙花数
Print
Print "水仙花数为:";
For i = 0 To 10000
a = i \ 1000
b = (i - a * 1000) \ 100
c = (i - a * 1000 - b * 100) \ 10
d = i - a * 1000 - b * 100 - c * 10
If i = a ^ 3 + b ^ 3 + c ^ 3 + d ^ 3 Then
Print i;
End If
Next i
End Sub