题解 | #完全数计算#

完全数计算

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


public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        if(sc.hasNext()){
            //接收一个数
            int input = sc.nextInt();
            //用来统计完全数
            int count=0;
            for(int i =1;i<=input;i++){
                if(PerNum(i)){
                    count++;
                }
            }
            System.out.print(count);
        }
    }
    private static boolean PerNum(int num){
        //计算出来的因子,放在一个set集合中
        HashSet<Integer> set=new HashSet<>();
        //计算因子
        for(int j=1;j<=num;j++){
            if(num%j==0){
                //没有余数说明是因子
                set.add(j);
            }
        }
        int all=0;
        //对set集合进行遍历
        for(Integer integer:set){
            if(integer!=num){
                all+=integer;
            }
        }
        //再判断传进来的数和计算的数是否相同
        if(all==num){
            return true;
        }else{
            return false;
        }

    }
}
全部评论

相关推荐

我看看你怎么个事来
牛牛爱吃草草:我看看你怎么个事来
点赞 评论 收藏
分享
07-09 19:25
门头沟学院 Java
这是要把每一个投校招的都开盒吗?
26届之耻将大局逆转:裁人的时候一次性追回餐费
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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