您的位置:首页 > 编程语言 > Java开发

Java经典算法40例(三)

2017-07-09 14:55 453 查看
打印出所有的 “水仙花数 “,所谓 “水仙花数 “是指一个三位数,其各位数字立方和等于该数本身。例如: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: