Source
Problem Id:1016 User Id:C0400348198
Memory:16K Time:100MS
Language:C++ Result:Accepted
#include <stdio.h>
#include <string.h>
void inventory(const char * a, char * b){
int f[10];
int i, count;
for(i = 0; i < 10; i ++)
f[i] = 0;
for(i = 0; a[i] != 0; i ++)
f[a[i] - '0'] ++;
count = 0;
for(i = 0; i < 10; i ++)
if(f[i] > 0)
count += sprintf(b + count, "%d%d", f[i], i);
}
void main(){
char n[16][81];
int i, j;
while(1){
scanf("%s", n[0]);
if(strcmp("-1", n[0]) == 0) break;
for(i = 1; i <= 15; i ++){
inventory(n[i - 1], n[i]);
for(j = 0; j < i; j ++)
if(strcmp(n[i], n[j]) == 0){
if(i == 1)
printf("%s is self-inventorying\n", n);
else if(j == i - 1)
printf("%s is self-inventorying after %d steps\n", n, i - 1);
else
printf("%s enters an inventory loop of length %d\n", n, i - j);
break;
}
if(j < i) break;
}
if(i == 16)
printf("%s can not be classified after 15 iterations\n", n);
}
}