Private Sub Command1_Click()
Dim i As Integer
Dim a As Integer, b As Integer, c As Integer
For i = 100 To 999
a = i \ 100 '求三位数的百位上的数
b = (i - a * 100) \ 10 '求三位数的十位上的数
c = i - a * 100 - b * 10 '求三位数的个位上的数
If a ^ 3 + b ^ 3 + c ^ 3 = i Then '判断是否为水仙花数
Print i; '如果是水仙花数则输出.
End If
Next i
End Sub
用三重循环做:
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer
Dim s As Integer
For i = 1 To 9
For j = 0 To 9
For k = 0 To 9
s = i * 100 + j * 10 + k
If i ^ 3 + j ^ 3 + k ^ 3 = s Then
Print s;
End If
Next k
Next j
Next i
End Sub
Private Sub Command1_Click()
Dim i as long, a as long, b as long, c as long
For i = 100 To 999
a = Val(Left(i, 1))
b = Val(Mid(i, 2, 1))
c = Val(Right(i, 1))
If i = a ^ 3 + b ^ 3 + c ^ 3 Then list1.AddItem i
Next
End Sub