Nerde Nolzda

ZeroJudge: a010 因數分解

從 2 開始一個一個除。
Start dividing from 2.

ZeroJudge Link (Zh)

#include <stdio.h>

main() {
  int input;
  while (scanf("%d", &input) != EOF) {
    int i, j = 0;
    int buffer[100];
    while (input != 1) {
      for (i = 2; i <= input; i++) {
        if (input % i == 0) {
          input /= i;
          buffer[j++] = i;
          break;
        }
      }
    }
    if (j == 0) printf("1");
    else {
      int k;
      for (k = 0; k < j; k++) {
        int count = 1;
        while ((k + count) < j && buffer[k] == buffer[k+count])
          count++;
        if (count == 1) printf("%d", buffer[k]);
        else printf("%d^%d", buffer[k], count);
        k += count - 1;
        if ((k + 1) < j) printf(" * ");
      }
    }
    printf("\n");
  }
  return 0;
}

Related Posts

0 comments

Post a comment

Send an email to comment@nerde.pw.