水仙花数
水仙花数
水仙花数是指一种三位数,其各个数之立方和等于该数。
水仙花数共有4个,分别为:153、370、371、407 ( 例如:1^3 + 5^3 + 3^3 = 153 )
C语言的"水仙花数"实现代码:
void main()
{
int ge,shi,bai,number;
for(number=100;number<1000;number++) {
bai=number/100;
shi=(number%100)/10;
ge=number%10;
if(number==bai*bai*bai+shi*shi*shi+ge*ge*ge) printf("\nnumber=%d\n",number);
}
getch();
}
Visual Basic的"水仙花数"实现代码:
Private Sub Form_Click()
Dim a, b, c As Integer 'a(个)b(十)c(百)
For a = 0 To 9
For b = 0 To 9
For c = 1 To 9
If a ^ 3 + b ^ 3 + c ^ 3 = a + 10 * b + 100 * c Then
MsgBox 100 * c + 10 * b + a
End If
Next c
Next b
Next a
End Sub
FORTRAN的"水仙花数"实现代码:
WRITE(*,30)
DO 10 K=100,999
IA=K/100
IB=MOD(K,100)/10
IC=MOD(K,10)
IF(K.NE.IA**3+IB**3+IC**3) GOTO 10
WRITE(*,20)K, IA,IB,IC
10 CONTINUE
20 FORMAT(5X,4I4)
30 FORMAT(5X,18HN=I**3+J**3+K**3)
STOP
END
pascal:
program shuixianhuashu; var
a,b,c:integer;
begin
for a:=1 to 9 do
for b:=0 to 9 do
for c:=0 to 9 do
if a*a*a+b*b*b+c*c*c=100*a+10*b+c then writeln(100*a+10*b+c);
用C语言for循环求解该问题的代码
#include "Stdio.h"
int main(void)
{
int a,b,c;
for(a=1;a<=9;a++)
for(b=0;b<=9;b++)
for(c=0;c<=9;c++)
if(100*a+10*b+c==a*a*a+b*b*b+c*c*c)
printf("%d%d%d ",a,b,c);
}
end.