对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”。
给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False
示例:
输入: 28
输出: True
解释: 28 = 1 + 2 + 4 + 7 + 14
注意:
输入的数字 n 不会超过 100,000,000. (1e8)
详见:https://leetcode.com/problems/perfect-number/description/

C++:

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

参考:http://www.cnblogs.com/grandyang/p/6636879.html

507 Perfect Number 完美数的更多相关文章

  1. [LeetCode] 507. Perfect Number 完美数字

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

  2. 【leetcode】507. Perfect Number

    problem 507. Perfect Number solution: /* class Solution { public: bool checkPerfectNumber(int num) { ...

  3. 507. Perfect Number

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

  4. [LeetCode] Perfect Number 完美数字

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

  5. 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 507. Perfect Number 因数求和

    [抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...

  7. [Swift]LeetCode507. 完美数 | Perfect Number

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

  8. C#LeetCode刷题之#507-完美数(Perfect Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3879 访问. 对于一个 正整数,如果它和除了它自身以外的所有正因 ...

  9. Java实现 LeetCode 507 完美数

    507. 完美数 对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为"完美数". 给定一个 整数 n, 如果他是完美数,返回 True,否则返回 False ...

随机推荐

  1. nodejs的request模块

    request模块让http请求变的更加简单.(作为客户端,去请求.抓取另一个网站的信息) request的GitHub主页: https://github.com/request/request 最 ...

  2. rails elasticsearch searchkick用法

    1.安装elasticsearch 之前要先安装java8: 参考https://www.elastic.co/guide/en/elasticsearch/reference/current/zip ...

  3. 超全!整理常用的iOS第三方资源(转)

    超全!整理常用的iOS第三方资源 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地 ...

  4. mysql数据库引擎InnoDB和MyISAM的区别

    InnoDB支持行级锁和表级锁(默认行级锁),支持事务,外部键等:大量的insert和update更快等.只有通过索引条件检索数据,InnoDB 才使用行级锁,否则,InnoDB 将使用表锁. MyI ...

  5. poj 3461 Oulipo(kmp统计子串出现次数)

    题意:统计子串出现在主串中的次数 思路:典型kmp #include<iostream> #include<stdio.h> #include<string.h> ...

  6. Unbuntu 终端中使用Tab键不能自动补全

    解决方案 1.利用vi编辑器打开 /etc/bash.bashrc文件(需要root权限) sudo vi /etc/bash.bashrc 2.找到文件中的下列代码 #enable bash com ...

  7. MD5Util1

    package com.cc.hkjc.util; import java.math.BigInteger;import java.security.MessageDigest;import java ...

  8. (转)Vim自动补全神器:YouCompleteMe

    原文出处:http://blog.jobbole.com/58978/ 第一次听说这个插件还是在偶然的情况下看到别人的博客,听说了这个插件的大名.本来打算在实训期间来完成安装的,无奈网实在不给力,也就 ...

  9. 「LuoguP2170」 选学霸(01背包

    Description 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一部分没有,同学们就会抗议.所以老师想请你帮他求出他该选多少学霸,才能既不让同学们抗议, ...

  10. Dijkstra堆优化

    Dijkstra是一个非常不错的最短路算法,它使用两层循环进行枚举,通过每次更新蓝白点的方式更新最短路,时间复杂度为O(n^2),优于floyd的O(n^3),不过只能用于计算单源最短路,而且无法处理 ...