http://acm.pku.edu.cn/JudgeOnline/problem?id=1016
Код:
#include <stdio.h> #include <string.h> #define MAXL 200 char stack[MAXL][MAXL]; int top; char t1[MAXL],t2[MAXL]; int count[10]; int flag; void push(char *s,int &t) { strcpy(stack[t],s); t++; } int main() { int i,j,k,l,o; char tmp[MAXL]; scanf("%s",t1); while (strcmp(t1,"-1") != 0) { top = 0; flag = -1; while (top < 15) { push(t1,top); memset(count,0,sizeof(count)); o = strlen(t1); for (i = 0; i < o; i++) count[t1[i]-'0']++; l = 0; for (i = 0; i < 10; i++) if (count[i]) { k = 0; do { tmp[k++] = count[i] % 10 + '0'; count[i] /= 10; } while (count[i]); for (j = k - 1; j >= 0; j--) t2[l++] = tmp[j]; t2[l++] = i + '0'; } t2[l] = '\0'; if (strcmp(t1,t2) == 0) { flag = 0; break; } for (i = top - 2; i >= 0; i--) if (strcmp(t2,stack[i]) == 0) { flag = 1;k = i; break; } strcpy(t1,t2); } if (flag == -1) printf("%s can not be classified after 15 iterations\n",stack[0]); else if (flag == 1) printf("%s enters an inventory loop of length %d\n",stack[0],top - k); else if (top == 1) printf("%s is self-inventorying\n",stack[0]); else printf("%s is self-inventorying after %d steps\n",stack[0],top-1); scanf("%s",t1); } return 0; }