题解 | #完全数计算#
完全数计算
http://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
#include <bits/stdc++.h>
using namespace::std;
bool isPerfectNum(const int &num, unordered_set<uint> &us)
{
int sum = 0;
for (auto const &v : us) {
sum += v;
}
return sum == num;
}
void getPrime(const int &i, unordered_set<uint> &us)
{
for (int j = 2;j <= sqrt(i);j++) {
if (0 == i % j) {
us.insert(j);
us.insert(i / j);
}
}
}
int main()
{
uint num = 0;
while (cin >> num) {
int cnt = 0;
unordered_set<uint> us;
for (int i = 2;i <= num;i++) {
us.clear();
us.insert(1);
getPrime(i, us);
if (isPerfectNum(i, us)) {
cnt++;
}
}
cout << cnt << endl;
}
return 0;
}
using namespace::std;
bool isPerfectNum(const int &num, unordered_set<uint> &us)
{
int sum = 0;
for (auto const &v : us) {
sum += v;
}
return sum == num;
}
void getPrime(const int &i, unordered_set<uint> &us)
{
for (int j = 2;j <= sqrt(i);j++) {
if (0 == i % j) {
us.insert(j);
us.insert(i / j);
}
}
}
int main()
{
uint num = 0;
while (cin >> num) {
int cnt = 0;
unordered_set<uint> us;
for (int i = 2;i <= num;i++) {
us.clear();
us.insert(1);
getPrime(i, us);
if (isPerfectNum(i, us)) {
cnt++;
}
}
cout << cnt << endl;
}
return 0;
}