求因子和并与原数比较,这里是最水的枚举求因子,好像有效率更高的方法,不过这边数据量比较小,土办法即可
#include<stdio.h>
#include<math.h>
int factor_sum(int num)
{
if (num == 1)
return 0;
int i, sum = 1, n = (int) sqrt(num);
for (i = 2; i <= n; i++)
if (num % i == 0)
{
int div = num / i;
sum = i != div ? sum + i + div : sum + i;
}
return sum;
}
int main()
{
printf("PERFECTION OUTPUT\n");
int n;
while (scanf("%d", &n), n)
{
printf("%5d ", n);
int sum = factor_sum(n);
if (sum > n)
printf("ABUNDANT\n");
else if (sum < n)
printf("DEFICIENT\n");
else
printf("PERFECT\n");
}
printf("END OF OUTPUT\n");
return 0;
}