所谓的水仙花数(梅花数)是指在三位整数(100到999之间)中,百字0位数、十位数、个位数的立方和等于它本身,如字0153=1^3+5^3+3^3。程序代码如下:
Private Sub Command1_Click() Dim i As Integer, s As Integer Dim a As Integer, b As Integer, c As Integer Print "100到999所有水仙花数(也叫梅花数):"
; For i = 100 To 999 a = i \ 100 '取百位数 b = i \10 Mod 10 '或 b = i Mod 100 \10 取十位数 c = i Mod 10 ‘取个位数 s = a ^ 3 + b ^ 3 + c ^ 3 '水仙花数的判断依据 If s = i Then Print i; End If Next iEnd Sub运行结果:100到999所有水仙花数(也叫梅花数): 153 370 371 407。