2013年8月19日

UVa 12414 - Calculating Yuan Fen

#include <stdio.h>
#include <string.h>

int check(char * s) {
  return !s[3] || !strcmp(s, "100");
}

int solve(char * s) {
  while (!check(s)) {
    int i;
    for (i = 0; s[i + 1]; i++) {
      s[i] = (s[i] - '0' + s[i + 1] - '0') % 10 + '0';
    }
    s[i] = 0;
  }
  return !strcmp(s, "100");
}

int main() {
  char s[99];
  while (gets(s)) {
    int i, ST, solved = 0;
    for (ST = 1; ST <= 10000 && !solved; ST++) {
      char temp[999] = {};
      for (i = 0; s[i]; i++) {
        char num[99];
        sprintf(num, "%d", ST + s[i] - 'A');
        strcat(temp, num);
      }
      solved = solve(temp);
      if (solved) {
        printf("%d\n", ST);
      }
    }
    if (!solved) {
      puts(":(");
    }
  }
  return 0;
}

沒有留言:

張貼留言