完美的数字

题目:

 * 输入正整数N,检查它是否完美输出YES或者NO。
 * 把一个数字的每一位拆分开,计算他们的阶乘再累加,如果和等于原数字,则该数字是完美的。
 * eg:
 * 145
 * 1 + 4*3*2*1 + 5*4*3*2*1  == 145
---------------------------------------------
阶层的意思:5! =   5的阶层  =   5*4*3*2*1

做法:

class Test61 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();
        scanner.close();
        if (isPerfectNumber(number)) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }

    public static boolean isPerfectNumber(int number) {
        int total = 0;
        String s = String.valueOf(number);
        for (int i = 0; i < s.length(); i++) {
            Integer n = Integer.valueOf(String.valueOf(s.charAt(i)));
            int r = jieCen(n);
            total += r;
        }
        if (total == number){
            return true;
        }else {
            return false;
        }
    }

    //一个数的阶层
    public static int jieCen(int number) {
        int sum = 1;
        for (int i = 1; i <= number; i++) {
            sum *= i;
        }
        return sum;
    }

}

posted on 2023-06-08 21:43  陈嘻嘻-  阅读(10)  评论(0编辑  收藏  举报

导航