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
相关文章推荐
- Java经典算法40例(二)
- Java经典算法40例(九)
- Java经典算法40例(二十三)
- Java经典算法40例(三十一)
- Java经典算法40例(二十七)
- Java经典算法40例(五)
- Java经典算法40例(十八)
- Java经典算法40例(二十四)
- Java经典算法40例(二十)
- Java经典算法40例(三十)
- Java经典算法40例(七)
- Java经典算法40例(十六)
- Java经典算法40例(十一)
- Java经典算法40例(二十五)
- JAVA经典算法40例
- Java经典算法40例(八)
- Java经典算法40例(十四)
- Java经典算法40例(十五)
- Java经典算法40例(二十八)
- Java经典算法40例(二十六)