题解 | #完全数计算#

完全数计算

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Stream.*;


public class Main {
    public static void main(String[] args) throws IOException {

        //testCompletePack();
        testTh();
    }

    private static void testTh() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = bf.readLine()) != null) {
            int parseInt = Integer.parseInt(str);
            HashMap<Integer, Integer> hp = new HashMap<>();
            int count = 0;
            for (int j = 1; j < parseInt; j++) {
                double sqrt = Math.sqrt(j);
                for (int i = 1; i < sqrt; i++) {
                    if (j % i == 0) {
                        hp.put(i, j / i);
                    }
                }
                Set<Map.Entry<Integer, Integer>> entries = hp.entrySet();
                int res = 0;
                for (Map.Entry<Integer, Integer> entry : entries) {
                    Integer key = entry.getKey();
                    Integer value = entry.getValue();
                    //System.out.println("key:+"+key+"value:+"+value);
                    if (value == j) {
                        res = res + key;
                        continue;
                    }
                    res = res + key + value;
                }
                hp.clear();
                if (res == j) count++;
            }
            System.out.println(count);
        }

    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
简历当中有水分算不算造假...
点赞 评论 收藏
分享
06-23 11:43
门头沟学院 Java
allin校招的烤冷...:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
07-02 13:52
武汉大学 golang
骗你的不露头也秒
牛客87776816...:😃查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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