【java练习题3】--水仙花数
【程序3】 ??
题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。 ??
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
2.
public static boolean sxh(int p){
int bw=p/100;
int gw=p%10;
int sw=(p-bw*100-gw)/10;
if(p==(Math.pow(bw, 3)+Math.pow(sw, 3)+Math.pow(gw, 3))){
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=100;i<=999;i++){
boolean flag=sxh(i);
if(flag){
System.out.println(i);
}
}
}
1 楼 mfkvfn 2012-04-25 你的代码只能得出正确结果,性能就不怎么样了。