poj1016

来源:互联网 发布:知乎 管理者 编辑:程序博客网 时间:2024/05/22 16:05
#include<iostream>
#include<string>
using namespace std;
string record[16] = {""};
string num;






int main()
{
cin >> num;
while (num != "-1")
{
int cha = 0;
int sta = 0;
record[0] = num;
for(int i=0;i<15;i++)
{
int length = 0;
while(record[i][length]!='\0')
length++;
int array[10] = { 0 };
for(int j=0;j<length;j++)
{

int mark = record[i][j] - '0';
array[mark]++;
}
string temp = "";
for(int j=0;j<10;j++)
{
if(array[j]!=0)
{
string geshu="", shu="";
if (array[j] < 10)
geshu = array[j] + '0';
else
{
geshu = array[j]/10 + '0';
geshu += (array[j] % 10) + '0';
}
shu = j + '0';
temp += geshu;
temp += shu;
}
}
record[i+1] = temp;
for(int j=0;j<=i;j++)
{
if(record[j]==temp)
{
cha = i + 1 - j;
sta = j;
break;
}
}

if(cha==1)
{
if (sta == 0)
{
cout << num << " is self-inventorying" << endl;
}
else cout << num << " is self-inventorying after " << sta << " steps" << endl;
break;
}
else if(cha!=0)
{
cout << num << " enters an inventory loop of length " << cha<<endl;
break;
}

}
if (cha == 0)
cout << num << " can not be classified after 15 iterations" << endl;
cin >> num;
}

return 0;


}