题解 | #完全数计算#
完全数计算
http://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
//代码
#include<iostream>
#include<set>
using namespace std;
int sum (set<int>& num) {
int result = 0;
for (auto j : num)
result = j + result;
return result;
}//set求和
bool perfect(int num) {
if (num == 1)
return false;
set<int> yinzi;
yinzi.insert(1);
for (int i = 2; i * i < num; i++) {
if (num % i == 0) {
yinzi.insert(i);
yinzi.insert(num / i);
}
}
if (sum(yinzi) == num)
return true;
return false;
}//判断是否为完全数
int main() {
int n;
cin >> n;
int count;
for (int i = 1; i <= n; i++) {
if (perfect(i))
count++;
}
cout << count << endl;
return 0;
}