题解 | #完全数计算#
完全数计算
https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
#include <iostream> #include <cmath> #include <vector> using namespace std; bool perfectNumber(int m) { if(m==1) return false; vector<int> divisors; divisors.push_back(1); int l = sqrt(m); for(int i = 2;i<=l;++i) { if(m%i==0) { divisors.push_back(i); int t = m/i; if(t!=i) divisors.push_back(t); } } l=0; for(auto &i:divisors) { l+=i; } if(l==m) return true; return false; } int howManyPerfectNumber(int n) { int count = 0; for(int i = 1;i!=n+1;i++) { if(perfectNumber(i)) ++count; } return count; } int main() { int n; cin>>n; cout<<howManyPerfectNumber(n); } // 64 位输出请用 printf("%lld")