水仙花数是指一种三位数,其各个数之立方和等于该数。
水仙花数共有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.