7.Perfect Number

Posted legooooo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
};

 

以上是关于7.Perfect Number的主要内容,如果未能解决你的问题,请参考以下文章