贴这个代码纯碎是为了表达c++真的很优美
#include <iostream>
#include <cstring>
using namespace std;
string str[16];
void process(string input ,int index)
{
if(index==16) return;
int number[10]={0 };
for(int i=0;i<input.length();i++)
{
number[input[i]-'0']++;
}
string temp;
for(int i=0;i<10;i++)
{
if(number[i]>0)
{
if(number[i]<10)
{
temp.insert(temp.end(),number[i]+'0');
}
else
{
temp.insert(temp.end(),number[i]/10+'0');
temp.insert(temp.end(),number[i]%10+'0');
}
temp.insert(temp.end(), i+'0');
}
}
str[index]=temp;
process(temp,index+1);
}
int find_loop(int index)
{
for(int i=index+1;i<16;i++)
{
if(str[index]==str[i])
{
return i-index;
}
}
return 0;
}
int main()
{
string input;
while(cin>>input)
{
if(input=="-1")break;
str[0]=input;
process(input,1);
bool after = false;
if(str[0]==str[1])
{
cout<<input<<" is self-inventorying" << endl;
}
else
{
for(int i=1;i<=15;i++)
{
if(str[i]==str[i-1])
{
after=true;
cout << input <<" is self-inventorying after " << i-1 <<" steps" << endl;
break;
}
}
int loop=0;
if(!after)
{
for(int i=0;i<=15;i++)
{
loop=find_loop(i);
if(loop>0)
{
cout << input <<" enters an inventory loop of length "<< loop << endl;
break;
}
}
}
if(!after&&loop==0)
{
cout << input << " can not be classified after 15 iterations" << endl;
}
}
}
}