算法练习3---水仙花数java版

所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。

java程序代码如下:

public class ArithTest {
    public static void main(String[] args) {
        ArithTest at = new ArithTest();
        //打印所有的水仙花数
        System.out.println("水仙花数:");
        for(int a=100;a<=999;a++){
            boolean bl = at.fk(a);
            if(bl){
                System.out.println(a);
            }
        }
    /* 水仙花数
     * 所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。
     * 打印所有水仙花数
     */
    public boolean fk(int a){
        int x = a/100;
        int y = a%100/10;
        int z = a%10;
        int k = x*x*x+y*y*y+z*z*z;
        if(a == k){
            return true;
        } else {
            return false;
        }
    }
}

执行结果如下:

水仙花数:
153
370
371
407

 

posted @ 2017-01-20 16:32  泡泡圈  阅读(356)  评论(0编辑  收藏  举报