ITPub博客

首页 > IT职业 > IT职场 > #6.求水仙花数

#6.求水仙花数

IT职场 作者:solution 时间:2021-09-09 10:30:23 0 删除 编辑
public class TestNNN {
    static int a = 0;
    static int b = 0;
    static int c = 0;
    public static void main(String[] args) {
        /**
         * 水仙花数
         * 1. 一定是3位数
         * 2. 每一位的立方,加起来恰好是这个数本身,比如153=1*1*1+5*5*5+3*3*3
         */
        for (int i = 100; i < 999; i++) {
            a = i / 100; //百位
            b = i / 10 % 10; //十位
            c = i % 10; //个位
            double sum = Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3);
            if (sum == (double) i) {
                System.out.println("水仙花数有:" + i);
            }
        }
    }
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2459/viewspace-2822872/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 使用教程
请登录后发表评论 登录
全部评论

注册时间:2019-08-05

  • 博文量
    101
  • 访问量
    21772