码迷,mamicode.com
首页 > 其他好文 > 详细

7.Perfect Number

时间:2017-04-09 22:38:07      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:bool   strong   script   and   for   number   cep   ==   function   

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

标签:bool   strong   script   and   for   number   cep   ==   function   

原文地址:http://www.cnblogs.com/sarahp/p/6686762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!