前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。这次比赛略无语,没想到前3题都可以用暴力解。
博客链接:mcf171的博客
——————————————————————————————
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返回false...public class Solution {
public boolean checkPerfectNumber(int num) {
if(num == 1) return false;
int sum = 1;
int up = (int)Math.sqrt(num);
for(int i = 2; i <= up; i++){
if((num % i) == 0) {sum += i; sum += num / i;}
}
return sum == num;
}
}