java水仙花数,java水仙花数代码for循环

发布时间:2022-12-01

本文目录一览:

  1. java求水仙花数,怎么样统计水仙花数的个数啊?
  2. 用Java写个关于“水仙花数”的程序?
  3. Java 编程找出所有的水仙花数(水仙花数)。
  4. java水仙花数?
  5. java中求解水仙花数的算法思想是什么?

java求水仙花数,怎么样统计水仙花数的个数啊?

统计水仙花数的个数的话可以使用一个变量来计数,循环判断如果这个数是水仙花数的话,就把这个数加1。 具体示例代码如下:

public class Demo1 {
    public static void main(String[] args) {
        System.out.println("100~999之间的水仙花数是:");
        int count = 0;
        for (int custNo = 100; custNo <= 999; custNo++) {
            int geWei = custNo % 10;
            int shiWei = custNo / 10 % 10;
            int baiWei = custNo / 100 % 10;
            if (custNo == geWei * geWei * geWei + shiWei * shiWei * shiWei + baiWei * baiWei * baiWei) {
                count++;
                System.out.println(custNo);
            }
        }
        System.out.println("个数:" + count);
    }
}

接下来介绍下什么是水仙花数: 水仙花数是指一个n位数(n≥3),它的每个位上的数字的n次幂之和等于它本身。

用Java写个关于“水仙花数”的程序?

按一下代码执行:

public class woo {
    public static void main(String args[]) {
        System.out.println("100-1000中的水仙花数有:");
        for (int i = 100; i < 1000; i++) {
            int single = i % 10;
            int ten = i / 10 % 10;
            int hundred = i / 10 / 10 % 10;
            // 水仙花数判断要求
            if (i == (single * single * single + ten * ten * ten + hundred * hundred * hundred)) {
                System.out.println(i);
            }
        }
    }
}

扩展资料: 水仙花数只是自幂数的一种,严格来说3位数的3次幂数才称为水仙花数。

  • 一位自幂数:独身数
  • 两位自幂数:没有
  • 三位自幂数:水仙花数
  • 四位自幂数:四叶玫瑰数
  • 五位自幂数:五角星数
  • 六位自幂数:六合数
  • 七位自幂数:北斗七星数
  • 八位自幂数:八仙数
  • 九位自幂数:九九重阳数
  • 十位自幂数:十全十美数 参考资料:
  • 水仙花数——百度百科

Java 编程找出所有的水仙花数(水仙花数)

代码如下:

package com.vo;
public class Shuixianhua {
    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        int c = 0;
        for (int i = 100; i < 999; i++) {
            a = i / 100;
            b = i / 10 % 10;
            c = i % 10;
            if (i == (a * a * a + b * b * b + c * c * c)) {
                System.out.println(i);
            }
        }
    }
}

首先,“水仙花数”是指一个三位数,其各位数字立方和等于该数, 如:153是一个“水仙花数”,因为1的三次方+5的三次方+3的三次方等于153。 解此题的关键是怎能样从一个三位数中分离百位数、十位数、个位数。可以这样做,设该三位数以i代表,由a,b,c三个数字组成。

  1. 百位数字a: a = (int)(i / 100)
  2. 十位数字b: b = (int)((i - 100 * a) / 10)
  3. 个位数字c: c = i - (int)(i / 10) * 10 然后在根据上述代码可得。 扩展资料: 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。 参考资料:水仙花数百度百科

java水仙花数?

100以内是没有水仙花数的! 水仙花数定义:水仙花数是指一个n位数(n≥3),它的每个位上的数字的n次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)

// 100-999之间的数
public class Demo {
    public static void main(String[] a) {
        for (int i = 101; i < 999; i++) {
            String s = new Integer(i).toString();
            int temp = 0;
            int n = s.length();
            for (int j = 0; j < n; j++) {
                temp += Math.pow(Character.digit(s.charAt(j), 10), n);
            }
            if (temp == i) {
                System.out.println(i);
            }
        }
    }
}

不太清楚你所说的100间的斐波那契数是什么概念,是100以内的斐波那契数还是100步以内的斐波那契数,下面给你写的程序里面n是步数:

public class Demo {
    public static void main(String[] args) {
        int n = 20;
        fib(n);
    }
    public static void fib(int n) {
        long f1 = 1, f2 = 1, fn = 0;
        if (n == 1) System.out.print(f1);
        if (n == 2) System.out.print(f2);
        else {
            System.out.print(f1 + " ");
            System.out.print(f2 + " ");
            for (int i = 2; i < n; i++) {
                fn = f1 + f2;
                f1 = f2;
                f2 = fn;
                System.out.print(fn + " ");
            }
        }
    }
}

java中求解水仙花数的算法思想是什么?

水仙花数是指一个n位数(n≥3),它的每个位上的数字的n次幂之和等于它本身。 从上面的定义可以看出,只要将给出的数字各个位数的数字分解出来,然后把个数字的3次方相加与原数相比是否相等即可判断出是否为水仙花数,给你一段源码,是求100~1000内的水仙花数,有注释,希望对你有帮助:

public class Wflower {
    public static void main(String[] args) {
        int a = 0, b = 0, c = 0;
        System.out.println("水仙花数是:");
        for (int i = 100; i < 1000; i++) { // 遍历所有3位数
            a = i / 100; // 获取3位数中百位的数
            b = i % 100 / 10; // 获取3位数中十位的数
            c = i % 100 % 10; // 获取3位数中个位的数
            a = a * a * a; // 计算第一位数的立方
            b = b * b * b; // 计算第二位数的立方
            c = c * c * c; // 计算第3位数的立方
            if ((a + b + c) == i) // 如果符合水仙花数
                System.out.print(" " + i);
        }
    }
}