• 7.Perfect Number


    Description:

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.

    Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.

    解题思路:

      直接求出所有约数然后相加,结果与目标数字相等即可。

    class Solution {
    public:
        bool checkPerfectNumber(int num) {
            if(num==1 || num<=0) return false;
            int sum=1;
            int temp=num;
            for(int i=2;i<temp;i++){
                if(num%i==0){
                    sum+=i;
                    sum+=num/i;
                    temp+=num/i;
                }
            }
            if(num==sum) return true;
            return false;
        }
    };

  • 相关阅读:
    linux echo 换行
    linux 脚本 逻辑关系的写法及区别
    linux vim ***
    跟我一起学Makefile
    linux awk
    linux grep命令 ***
    unbuntu 安装及服务器配置
    linux 静态库文件
    samba 配置
    linux tar
  • 原文地址:https://www.cnblogs.com/sarahp/p/6686762.html
Copyright © 2020-2023  润新知