题解 | #完全数计算#

完全数计算

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

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

public class Main {

    public static void main (String [] args) throws IOException {
        // Use BufferedReader rather than RandomAccessFile; it's much faster
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

        String line = null;
        while ((line = f.readLine()) != null)
        {
            int n = Integer.parseInt(line);
            int count = 0;
            while (n > 0)
            {
                int sum = 0;
                for (int i = 1; i < n; i++) 
                {
                    if (n % i == 0) sum += i;
                    if (sum > n) break;
                }
                if (sum == n) 
                {
                    count++;
                }
                n--;
            }
            out.println(count);
        }
        out.close();
        f.close();
    }
}
全部评论

相关推荐

海康威视 软开岗 15k15
点赞 评论 收藏
分享
9 4 评论
分享
牛客网
牛客企业服务