题解 | #完全数计算#

完全数计算

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

import java.util.*; 
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        //完全数都是偶数
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String s = br.readLine();
            int num = Integer.valueOf(s);
            int sum = 0, count = 0;
            for (int i = 3; i <= num; i++) {
                if (i % 2 != 0) {
                    continue;
                } else {
                    for (int j = 1; j <= i / 2; j++) {
                        if (i % j == 0) {
                            sum += j;
                        }
                    }
                    if (sum == i) {
                        //System.out.println(i);
                        count++;
                    }
                    sum = 0;
                }
            }
            System.out.println(count);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

全部评论

相关推荐

12-15 14:16
门头沟学院 Java
回家当保安:发offer的时候会背调学信网,最好不要这样。 “27届 ”和“28届以下 ”公司招聘的预期是不一样的。
实习简历求拷打
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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