皱纹纸牡丹花做法大全:用pascal编程 水仙花数问题

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 10:00:23
用pascal编程 水仙花数问题

水仙花数,就是指一个三位数,其各个数的立方和等于该数
声明4个整形变量,如a,b,c,d其中d为水仙花数
因为是个三位数,所以用a,b,c分别取他的百位,十位,个位
a=d/100;
b=(d%100)/10;
c=d/100%10;
for (d=100 to 999)
if (d==a*a*a+b*b*b+c*c*c)
输出d
我学的都是类c的,不懂pascal只能这样讲了.

我这是 100-999的数中,立方和等于该数的水仙花数的程序。
用temp来保存各位的立方的和。把for循环中的数100-999改成其他范围也行,但就不是水仙化数了。
var i,j,ans,num,temp:integer;
begin
for ans:=100 to 999 do
begin
num:=ans;
temp:=0;
repeat
temp:=temp+(sqr(num - (num div 10)*10))*(num-(num div 10)*10);
num:=num div 10;
until num=0;
if temp=ans then write(ans:4);
end;
end.