1. 首页
  2. 软件开发
  3. JAVA
  4. 用for循环求范围内水仙花数的

用for循环求范围内水仙花数的

  • 发布于 2024-09-21
  • 0 次阅读

水仙花数的特性:每位数的立方之和等于该数

 //求所选范围内所有的水仙花数
    public static void main(String[] args) {
        //定义
        int a = 0, b, c, d, e;
        System.out.println("输入要找水仙花数的范围");
        Scanner scanner = new Scanner(System.in);
        e = scanner.nextInt();
        //用for进行拆数
        //从1到输入的数每一个都用for进行拆数
        for (d = 1; d <= e; d++) {
            a = d % 10;//个位
            b = d / 10 % 10;//十位
            c = d / 100;//百位
            //和水仙花特性比对
            if (a * a * a + b * b * b + c * c * c == d) {
                //输出
                System.out.println(d);
            }
        }


    }
}

输出结果:

输入要找水仙花数的范围
99999
1
153
370
371
407
1000
1001

Process finished with exit code 0

原文链接: https://blog.csdn.net/daibadetianshi/article/details/136597176