题解 | #完全数计算#

完全数计算

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


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            int count = 0;
            for (int i = 1; i <= n; i++) {
                if (getPerfectNum(i)) {
                    count++;
                }
            }
            System.out.println(count);
        }
    }

    private static boolean getPerfectNum(int i) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int j = 1; j < i; j++) {
            if (i % j == 0) {
                list.add(j);
            }
        }
        int sum = 0;
        for (Integer integer : list) {
            sum += integer;
        }
        return sum == i;
    }

}
全部评论
6以内没有完全数吧
点赞 回复 分享
发布于 2022-08-01 10:53

相关推荐

用微笑面对困难:只要你保证项目和获奖都是真的就行尤其是“对战,总负责人”啊这些套职,基本上队员,打杂的都这么写
点赞 评论 收藏
分享
评论
7
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务