https://leetcode.com/problems/perfect-number/discuss/
题目:求完美数
代码一:直接判断
class Solution {
public:
bool checkPerfectNumber(int num) {
int sum=1;
for(int x=2;x<=sqrt(num);x++)
if(num%x==0) sum+=x,sum+=num/x;
return (sum==num&&num!=1);
}
};
代码二:打表
class Solution {
public:
bool checkPerfectNumber(int num) {
static unordered_set<int> n = {6, 28, 496, 8128, 33550336};
return n.count(num);
}
};