507. 完全数 Perfect Number

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)

  1. static public bool CheckPerfectNumber(int num) {
  2. if (num == 0 || num == 1) return false;
  3. int sum = 1;
  4. for (int i = 2; i < Math.Sqrt(num); i++) {
  5. if (num % i == 0) {
  6. sum += i + num / i;
  7. }
  8. }
  9. return sum == num;
  10. }

null

时间: 2024-12-28 19:07:01

507. 完全数 Perfect Number的相关文章

7.Perfect Number

Description: 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. 解题思

[LeetCode] Perfect Number 完美数字

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:

Codeforces Round #460 (Div. 2) 919A. Supermarket 919B. Perfect Number 919C. Seat Arrangements

这场cf有点意思,hack场,C题等于1的特判hack很多人(我hack成功3个人,上分了,哈哈哈,咳咳...) D题好像是树形dp,E题好像是中国剩余定理,F题好像还是dp,具体的不清楚,最近dp的题目好多,一会滚去学dp. 写A,B,C的题解. A. Supermarket time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

163.Perfect Number

题目: We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself. 我们定义Perfect Number是一个正整数,它等于除了它自己之外的所有正除数的总和. Now, given an integer n, write a function that returns true when it is a perfect

Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output We consider a positive integer perfect, if and only if the sum of its digits is exactly 1010. Given a positive integ

[Daily Coding Problem 70] Nth perfect number

This problem was asked by Microsoft. A number is considered perfect if its digits sum up to exactly 10. Given a positive integer n, return the n-th perfect number. For example, given 1, you should return 19. Given 2, you should return 28. This is one

E - Perfect Number

Problem description We consider a positive integer perfect, if and only if the sum of its digits is exactly 10. Given a positive integer k, your task is to find the k-th smallest perfect positive integer. Input A single line with a positive integer k

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

d010:盈数、亏数和完全数

题目: 对一个正整数N而言,将它除了本身以外所有的因子加起来的总和为S,如果S>N,则N为盈数,如果S<N,则N为亏数,而如果S=N,则N为完全数(Perfect Number).例如10的因子有1.2.5.10,1 +2+5=8<10,因此10为亏数,而12的因子有1.2.3.4.6.12,1+2+3+4+6=16>12,因此12为盈数.至于6的因子有1.2.3.6,1+2+3=6,所以6是完全数(它也是第一个完全数).现在请你写一个程序,输入一个正整数N,然后印出它是盈数.亏数