507 Perfect Number 完美数
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”。
给定一个 正整数 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 完美数的更多相关文章
- [LeetCode] 507. Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- 【leetcode】507. Perfect Number
problem 507. Perfect Number solution: /* class Solution { public: bool checkPerfectNumber(int num) { ...
- 507. Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- [LeetCode] Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 507. Perfect Number 因数求和
[抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...
- [Swift]LeetCode507. 完美数 | Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- C#LeetCode刷题之#507-完美数(Perfect Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3879 访问. 对于一个 正整数,如果它和除了它自身以外的所有正因 ...
- Java实现 LeetCode 507 完美数
507. 完美数 对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为"完美数". 给定一个 整数 n, 如果他是完美数,返回 True,否则返回 False ...
随机推荐
- CodeChef - COUNTARI FTT+分块
Arithmetic Progressions Given N integers A1, A2, …. AN, Dexter wants to know how many ways he can ch ...
- Rowkey is the Crux Rowkey Design
Apache HBase ™ Reference Guide http://hbase.apache.org/book.html#rowkey.design The Effect of ColumnF ...
- what's WSDL
WSDL (Web Services Description Language,Web服务描述语言) 它是一种XML Application,他将Web服务描述定义为一组服务访问点,客户端可以通过这些 ...
- 图解 servlet 与jsp的关系
Servlet是Java提供的用于开发Web服务器应用程序的一个组件,运行在服务器端,由Servlet容器所管理,用于生成动态的内容.Servlet是平台独立的Java类,编写一个Servlet,实际 ...
- svn服务器搭建与迁移
2016-11-21更新: 今天被svn的钩子搞了半天,网上找解决方法都无效,下午被我试出来了,特此记录. 在svn的钩子中可以使用update来更新配置文件,比如ansible的,puppet的,具 ...
- springboot使用thymeleaf 解析异常
在使用springboot的过程中,如果使用thymeleaf作为模板文件,则要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错!解决办法如下: 1.你可以使用严格的标签,也就是每 ...
- 常用文件操作模块json,pickle、shelve和XML
一.json 和 pickle模块 用于序列化的两个模块 json,用于字符串 和 python数据类型间进行转换 pickle,用于python特有的类型 和 python的数据类型间进行转换 Js ...
- [Selenium] Selenium 疑难杂症
1. jsclick 也不管用 Actions action = new Actions(driver); WebElement theRow = page.getInvisibleElement() ...
- 量子纠缠1——量子比特、Bell态、EPR佯谬
量子纠缠是量子物理的基本性质,他描述的是:当几个粒子相互作用后,无法单独描述各个粒子的性质,只能整体描述,本文主要介绍两个量子比特之间的纠缠. 量子比特(Qubit) 量子比特是量子计算的基本单位,就 ...
- k8s-helm-二十四
一.介绍 Helm是Kubernetes的一个包管理工具,用来简化Kubernetes应用的部署和管理.可以把Helm比作CentOS的yum工具. yum不光要解决包之间的依赖关系,还要提供具体的程 ...