对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”。
给定一个 正整数 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. hibernate面试点

    1.谈谈你对hibernate的认识和理解 01.全自动的ORM框架 02.子项目 03.面向对象的思想来解决操作数据库 01.hibernate是一个开放源代码的对象关系映射(ORM)框架,它对JD ...

  2. 逼近法(例 poj3208、poj1037)

    ​ 逼近法是一种很奇妙的算法,以为"逼近"这一种思想在很多的算法中都有体现.诸如:像我们的二分答案,不断地排除决策集合的一半以接近我们的最终答案:我们的树上倍增求 \(LCA\) ...

  3. HDU1495 非常可乐 —— BFS + 模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 非常可乐 Time Limit: 2000/1000 MS (Java/Others)    M ...

  4. ODC(Orthogonal Defect Classification)简介——正交缺陷分类法

    Defect分析是软件开发和测试中一个重要的环节,ODC介绍了一种不同于大家常用的非常有效的defect分类及分析方法.这篇文章简单的向大家介绍了什么是ODC,以及如何在项目和产品开发中使用ODC来改 ...

  5. jetty源码剖析

    最近使用jetty自己写了一个web server,现在闲了花了一天的时间看了一jetty的源代码,主要以server的启动为主线,进行了剖析,经过阅读对jetty的源码大赞,写的简洁.清晰,架构也不 ...

  6. UISwitch用法:

    代码: #import "ViewController.h" @interface ViewController () @end @implementation ViewContr ...

  7. web项目中url-pattern改成'/'后,js、css、图片等静态资源(404)无法访问问题解决办法

    感谢http://blog.csdn.net/this_super/article/details/7884383的文章 1.增加静态资源url映射 如Tomcat, Jetty, JBoss, Gl ...

  8. 笔记本电脑处理器(CPU)性能排行榜

    笔记本电脑处理器(CPU)性能排行榜 本排行榜随新款处理器(CPU)的发布而随时更新.更新日期:2012年7月15日   排名 型号 二级+三级缓存 前端总线(MHz) 功率(瓦) 主频(MHz) 核 ...

  9. 【215】◀▶ IDL 文件操作说明

    参考:I/O - General File Access Routines —— 基本文件操作函数 01   CD 修改当前的工作空间路径. 02   FILE_SEARCH 对文件名进行特定的查找. ...

  10. Ruby 全局变量,实例变量,类变量

    class Computer $manufacturer = "Mango Computer, Inc." # “$" 是全局变量关键字 @@num_of_instanc ...