题解 | #完全数计算#

完全数计算

https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84

import java.util.Scanner;

public class Main {

    public static boolean isPerfectNumber(int n) {
        int sum = 1;
        for (int i = 2; i < Math.sqrt(n); i++) {
            if (n % i == 0) {
                sum += n / i + i;
            }
        }
        return sum == n;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int total = 0;
        for (int i = 3; i < n; i++) {
            if (isPerfectNumber(i)) total++;
        }
        System.out.println(total);
    }
}

全部评论

相关推荐

漂亮的海豚在炒股:把西电加粗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务