题解 | #完全数计算#
完全数计算
https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
#include <stdio.h>
#include <stdbool.h>
bool perfect(int n) {
int f[10000];
int i_f = 1;
f[0] = 1;
for(int i = 2; i * i < n; i++) {
if(n % i == 0) {
f[i_f++] = i;
f[i_f++] = n / i;
}
}
int total = 0;
for(int i = 0; i < i_f; i++) {
total += f[i];
}
if(total == n) return true;
return false;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
int count = 0;
for(int i = 2; i < n; i++) {
if(perfect(i)) count++;
}
printf("%d\n", count);
}
return 0;
}
查看10道真题和解析