#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std ; const int MAXN = 100 ; char str[16][MAXN] ; char tmp[MAXN] ; int mark[MAXN] ; int ans1, ans2, len ; bool flags, flagi ; void judge () { int i, j ; for (i = 0; i < 15; i ++) { for (j = i+1; j < 16; j ++) { if (strcmp (str[i], str[j]) == 0 ) { if (j - i == 1) //标记self { flagi = true ; ans1 = i ; return ; } else //标记 enter { flags = true ; ans2 = j - i ; return ; } } } } } void solve () { int i, j ; int cnt ; for (i = 1; i <= 15; i ++) //15步,分别转换 { cnt = 0 ; memset (mark, 0, sizeof (mark)) ; for (j = 0; j < len; j ++) { mark[str[i-1][j] - '0'] ++ ; } for (j = 0; j < 10; j ++) { if (mark[j]) { if (mark[j] < 10) { str[i][cnt ++] = mark[j] + '0' ; str[i][cnt ++] = j + '0' ; } else if (mark[j] > 9) { str[i][cnt ++] = (mark[j]/10) + '0' ; str[i][cnt ++] = (mark[j]%10) + '0' ; str[i][cnt ++] = j + '0' ; } } } len = cnt ; } judge () ; } int main () { while (scanf ("%s", tmp) != EOF) { if (tmp[0] == '-') break ; strcpy (str[0], tmp) ; flags = false ; flagi = false ; ans1 = 0 ; ans2 = 0 ; len = strlen (tmp) ; solve () ; if (flagi) { if (ans1 == 0) printf ("%s is self-inventorying\n", str[0]) ; else printf ("%s is self-inventorying after %d steps\n", str[0], ans1) ; } else if (flags) printf ("%s enters an inventory loop of length %d\n", str[0], ans2) ; else printf ("%s can not be classified after 15 iterations\n", str[0]) ; memset (str, '\0', sizeof (str)) ; //错了无数多次的地方 } return 0 ; }URL : http://poj.org/problem?id=1016