打印出所有的 “水仙花数 “,所谓 “水仙花数 “是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 “水仙花数 “,因为153=1的三次方+5的三次方+3的三次方。
代码:
/**
* 水仙花数
* @author cheng
*
*/
public class Three {
public boolean flower(int m){ //判断是否为水仙花数
int a=m/100; //百位
int b=(m%100)/10; //十位
int c=m%10; //个位
if(a*a*a+b*b*b+c*c*c==m)
return true;
return false;
}
public static void main(String[] args){
Three three=new Three();
for(int m=100;m<=999;m++){
if(three.flower(m)==true)
System.out.println(m);
}
}
}
输出结果:
153
370
371
407