简单题。题目大意就是判断一个数是否为 prefect number。可是我竟然三次才 ac,原因是没有考虑到数字为 1 的时候。

求真因数和的时候可以考虑质数判断的算法,循环到 \(\sqrt{n}\) 就行了。代码很长很菜:

#include <stdio.h>
#include <math.h>

int get_sum(int number)
{
  int i;
  int sum = 0;

  for (i = 1; i < sqrt(number); ++i) {
    if (number % i == 0) {
      sum += i + number / i;
    }
  }

  if ((int)sqrt(number) * (int)sqrt(number) == number) {
    sum += sqrt(number);
  }

  sum -= number;

  return sum;
}

int main(int argc, char *argv[]) {
  int number;

  printf ("PERFECTION OUTPUT\n");

  while (scanf("%d", &number) && number) {
    int sum = get_sum(number);

    if (sum < number) {
      printf ("%5.0d  DEFICIENT\n", number);
    }
    else if (sum == number) {
      printf ("%5.0d  PERFECT\n", number);
    }
    else
      printf ("%5.0d  ABUNDANT\n", number);
  }

  printf ("END OF OUTPUT\n");

  return 0;
}