所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身,例如153是水仙花数,因为153=1*1*1+5*5*5+3*3*3.请编程求出所有的水仙花数
public class ShuiXianHua{
public static void main(String[] args){
for(int n=100;n<1000;n++){
int a=n/100;// 取得百位
int b=(n-a*100)/10;// 取得十位
int c=n % 10;// 取得个位
if(n==(a*a*a+b*b*b+c*c*c)){
System.out.println(n+"是水仙花数");
}
}
}
}
输出:
153是水仙花数
370是水仙花数
371是水仙花数
407是水仙花数
Thursday, April 23, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment