Numbers That Count
#include <iostream>
#include <string>
using namespace std;
string invent( string w );
int main()
{
string w;
while ( cin >> w && w != "-1" )
{
string seq[16] = { w };
for ( int i=1; i<=15; i++ )
seq[i] = invent( seq[i-1] );
int j, k;
for ( j=0; j<=15; j++ )
for ( k=1; j+k<=15; k++ )
if ( seq[j] == seq[j+k] )
goto found;
found:;
cout << w;
if ( j == 0 && k == 1 )
cout << " is self-inventorying";
else if ( j <= 15 && k == 1 )
cout << " is self-inventorying after " << j << " steps";
else if ( j <= 15 )
cout << " enters an inventory loop of length " << k;
else
cout << " can not be classified after 15 iterations";
cout << endl;
}
return 0;
}
string invent( string w )
{
int c[10] = { 0 };
for ( int i=0; i<(int)w.length(); i++ )
c[w[i]-'0']++;
string r;
for ( int i=0; i<10; i++ )
if ( c[i] > 0 )
{
if ( c[i] / 10 > 0 )
r += char( '0' + c[i]/10 );
r += char( '0' + c[i] % 10 );
r += char( '0' + i );
}
return r;
}