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.

Example:

Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14

Note: The input number n will not exceed 100,000,000. (1e8)

思路:

分解因子即可,注意只需要判断到sqrt(inputNum)即可,否则会超时。

   bool checkPerfectNumber(int num) {
if (num <= ) return false; int sum = ;
for (int i = ; i <=sqrt(num);i++)
{
if (num%i == )
{
sum += i;
sum += (num / i);
}
}
if (sum == num)
{
return true;
}
return false;
}

[leetcode-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. 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)

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

  4. 507. Perfect Number

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

  5. 507. Perfect Number 因数求和

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

  6. 507 Perfect Number 完美数

    对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”.给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False示例:输入: 28输出: True解释: ...

  7. LeetCode算法题-Perfect Number(Java实现)

    这是悦乐书的第249次更新,第262篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第116题(顺位题号是507).我们定义Perfect Number是一个正整数,它等于 ...

  8. [LeetCode] Perfect Number 完美数字

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

  9. LeetCode Perfect Number

    原题链接在这里:https://leetcode.com/problems/perfect-number/#/description 题目: We define the Perfect Number ...

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

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

随机推荐

  1. 区块链入门(1):搭建(Ubuntu系统)Truffle v3.2.1 开发和测试环境

    本文主要讲解ubuntu 16.04下, truffle开发测试环境的搭建.  第一步:安装nodejs 和 npm,有两种比较常见的方法. 方法1:直接在nodejs官网下载nodejs-v6.10 ...

  2. 用户登录安全框架shiro—用户的认证和授权(一)

     ssm整合shiro框架,对用户的登录操作进行认证和授权,目的很纯粹就是为了增加系统的安全线,至少不要输在门槛上嘛. 这几天在公司独立开发一个供公司内部人员使用的小管理系统,客户不多但是登录一直都是 ...

  3. 【设计模式】单一职责原则(SRP)

    单一职责原则是面向对象原则五大原则中最简单,也是最重要的一个原则, 他的字面定义如下: 单一职责原则(Single Responsibility Principle, SRP): 一个类只负责一个功能 ...

  4. 使用SpringBoot快速构建应用程序

    1.Spring MVC和Spring Boot自带的web构建方式有所区别.Spring提供了spring-boot-starter-web自动配置模块. 2. 添加如下依赖 <depende ...

  5. Struts2 Handle 404 error page and wrong action

    1. To handle 404 not found yourself, just add this code to your web.xml <error-page> <error ...

  6. Lua学习(1)——table

    table类型实现了“关联数组”.“关联数组”是一种具有特殊索引方式的数组.不仅可以通过证书来索引它,还可以使用字符串或其他类型(除了nil)来索引它.table是Lua中主要的数据结构机制(事实也是 ...

  7. 1.Node.js 接入微信公众平台开发

    一.写在前面的话   Node.js是一个开放源代码.跨平台的JavaScript语言运行环境,采用Google开发的V8运行代码,使用事件驱动.非阻塞和异步输入输出模型等技术来提高性能,可优化应用程 ...

  8. keyup实现在输入状态不发送搜索请求,停止输入后发送

    个人需求:通过keyup事件配合后台elasticsearch(弹性搜索),用户在输入状态不发送请求,等停止输入后发送请求. 这是个思考笔记,因为项目临时需要弹性搜索功能,所以临时想了这么个法子,方法 ...

  9. 基于查表的整数霍夫变换方法实现(matlab)

    暂时先用matlab把算法弄一下,这是基于查表的整数霍夫变换方法实现及解释. 接着再实现FPGA的霍夫变换. 霍夫变换原理和算法这里不多说,可参考以下链接: http://blog.csdn.net/ ...

  10. Node.js爬虫-爬取慕课网课程信息

    第一次学习Node.js爬虫,所以这时一个简单的爬虫,Node.js的好处就是可以并发的执行 这个爬虫主要就是获取慕课网的课程信息,并把获得的信息存储到一个文件中,其中要用到cheerio库,它可以让 ...