题解 | #完全数计算#
完全数计算
http://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
#include <stdlib.h>
#include <string.h>
int main(void)
{
int n;
int num = 0;
int *arr = (int *) malloc(sizeof(int) * 1000);
memset(arr, 0, sizeof(int) * 1000);
scanf("%d", &n);
for (int i = 1, k = 0, tmp = 0; i < n; ++i)
{
for (int j = 1; (j * j) < i; ++j)
{
if (i % j == 0)
{
arr[k++] = j;
}
}
for (int j = 0; j < k; ++j)
{
tmp += arr[j];
if (arr[j] != 1)
tmp += i / arr[j];
}
if (tmp == i)
{
//printf("#%d\n", tmp);
num++;
}
k = 0;
tmp = 0;
memset(arr, 0, sizeof(int) * 1000);
}
printf("%d", num);
free(arr);
return 0;
}