【华为机试056】完全数计算

题目描述:

完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数。

它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身。

例如:28,它有约数1、2、4、7、14、28,除去它本身28外,其余5个数相加,1+2+4+7+14=28。

给定函数count(int n),用于计算n以内(含n)完全数的个数。计算范围, 0 < n <= 500000

返回n以内完全数的个数。 异常情况返回-1

Java实现:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner  sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();

            int count = 0;
            for (int i = 2; i <= n; i++) {
                int sum = 1;
                for (int j = 2; j < Math.sqrt(i); j++) {
                    if (i % j == 0) {
                        sum += j;
                        sum += i/j;
                    }
                }
                if (sum == i) {
                    count++;
                }
            }

            System.out.println(count);
        }
    }
}

关键点:

  • 暴力破解。。。

猜你喜欢

转载自blog.csdn.net/heyiamcoming/article/details/81079518