POJ-1016

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

void change(char *t, char *f)
{
     int i;
     int num[10]={0};
     while (*t != '\0') {
           num[*t - '0']++;
           t++;
     }
     for (i = 0; i < 10; i++) {
         if (num[i] > 0) {
             if (num[i] > 9) {
                *(f++) = num[i]/10 + '0';
                num[i] %= 10;
             }
             *(f++) = num[i] + '0';
             *(f++) = i+'0';
         }
         *f = '\0';
     }
}
main()
{
      char str[16][81];
      int i, j, sign;
      for (scanf("%s", str[0]); strcmp(str[0], "-1");scanf("%s", str[0])) {
          change(str[0], str[1]);
          if (!strcmp(str[0], str[1])) {
             printf("%s is self-inventorying\n", str);
             continue;
          }
          for (i = 1, sign = 1; i < 15 && sign; i++) {
              change(str[i], str[i+1]);
              if (!strcmp(str[i], str[i+1])) {
                 printf("%s is self-inventorying after %d steps\n", str[0], i);
                 sign = 0;
                 break;
              }
              for (j = 0; j < i; j++)
                  if (!strcmp(str[j], str[i+1])) {
                     printf("%s enters an inventory loop of length %d\n", str[0], i+1-j);
                     sign = 0;
                     break;
                  }
          }
          if (sign)
             printf("%s can not be classified after 15 iterations\n", str[0]);
      } 
}

你可能感兴趣的:(POJ-1016)