Java基础练习:水仙花数

/**
 * @author cherhio
 * @date Created in 2019-05-05 10:44
 *
 *
 * 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
 * 例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
 */
public class NarcissisticNumber {
    public static void main(String[] args) {
        for (int i = 100; i <999 ; i++) {
            if (isNarcissisticNumber(i) == true){
                System.out.println(i);
            }
        }
    }

    public static boolean isNarcissisticNumber(int i){
        int hundred = i / 100;
        int ten = i / 10 % 10;
        int individual = i % 10;
        if (Math.pow(individual,3) + Math.pow(ten,3) + Math.pow(hundred,3) == i){
            return true;
        }
        return false;
    }
}

输出结果:
153
370
371
407

猜你喜欢

转载自blog.csdn.net/weixin_44355126/article/details/89841087