题解 | #完全数计算# C++带注释
完全数计算
https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
#include <iostream>
using namespace std;
int main() {
int n;
int res = 0;
cin >> n;
for(int i=1; i<=n; i++){
int j = i;
// lim灵活设置约数查找的边界,起到减枝和排除重复的作用。
int tp = 0,lim = j;
for(int t=1; t<lim; t++){
if(t*(j/t) == j){
if(t == 1){
tp += 1;
}else{
tp += t + (j/t);
// 一旦符合条件就调整边界。
lim = (j/t);
}
}
if(tp > j){
break;
}
}
if(tp == j){
res++;
// cout << j << endl;
}
}
cout << res <<endl;
return 0;
}
// 64 位输出请用 printf("%lld")
