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 ...
随机推荐
- springCloud和docker笔记(1)——微服务架构概述
1.微服务设计原则 1)单一职责原则:只关注整个系统中单独.有界限的一部分(SOLID原则之一) 2)服务自治原则:具备独立的业务能力和运行环境,可独立开发.测试.构建.部署 3)轻量级通信机制:体量 ...
- mongo-java-driver
http://mvnrepository.com/artifact/org.mongodb/mongo-java-driver/3.5.0 <!-- https://mvnrepository. ...
- JS是否确认提示 +页面跳转
JS友好提示 +页面跳转 function logout()...{if (confirm("你确定要注销身份吗?是-选择确定,否-选择取消"))...{window.locati ...
- throw 、throws 简介
抛出异常抛出异常有三种形式,一是throw,一个throws,还有一种系统自动抛异常.下面它们之间的异同.系统自动抛异常当程序语句出现一些逻辑错误.主义错误或类型转换错误时,系统会自动抛出异常.如: ...
- fork函数的使用【学习笔记】
#include "apue.h" ; char buf[] = "a write to stdout\r\n"; int main(void) { int v ...
- YTU 2421: C语言习题 矩形法求定积分
2421: C语言习题 矩形法求定积分 时间限制: 1 Sec 内存限制: 128 MB 提交: 354 解决: 234 题目描述 写一个用矩形法求定积分的通用函数,分别求 (说明: sin,co ...
- web开发中的mysql使用
一.单机mysql与mysql集群 1. 单机mysql很好理解,在一台物理机上安装好mysql服务端程序,使用这一台机器的硬件(cpu,内存,硬盘)进行数据的处理. 2.mysql集群 MySQL集 ...
- C++数组作为函数参数的几个问题(转)
本文需要解决C++中关于数组的2个问题:1. 数组作为函数参数,传值还是传址?2. 函数参数中的数组元素个数能否确定? 先看下面的代码. #include <iostream> using ...
- WebStorm配置SVN
下载SVN客户端管理工具TortoiseSVN-1.8.5.25224-x64-svn-1.8.8,选择合适的Windows版本 配置项目目录,对应的VCS为Subversion 设置Subversi ...
- 如何在单独的窗口中打开 Excel 文件
如何在单独的窗口中打开 Excel 文件 文章编号:087583 2012/11/1 18:45:29 故障现象: 如何在单独的窗口中打开 Excel 文件? 解决方案: 比较安全的方法就是直 ...