【算法003】水仙花数

打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。

水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。

package com.example.chyer.demo;

public class Test {

    public static void main(String[] args) {
        int single, ten, hundred;
        for (int i = 101; i < 1000; i++) {
            single = i % 10;
            ten = i / 10 % 10;
            hundred = i / 100;
            if (Math.pow(single, 3) + Math.pow(ten, 3) + Math.pow(hundred, 3) == i) {
                System.out.println(i);
            }
        }
    }
}

运行结果:

发布了29 篇原创文章 · 获赞 5 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/chyercn/article/details/103226440