题目大意:数据统计
看明白了,就是给你一个数,例如31123314,代表的意思有3个1,1个2,3个3,1个4,但数字本身的有的数字也是有3个1,1个2,3个3,1个4,所以这样的数叫做self-inventorying ,如果经过j步可以变成self-inventorying可以输出self-inventorying after j steps.如果经过j步变成的数跟前面的数一样那就输出enters an inventory loop of length 如果经过15次迭代还无法变成上面的分类,就输出can not be classified after 15 iterations。
明白意思应该不难了- -,做做去
学习了string 的应用
#include<iostream>
#include<string>
using namespace std;
#define maxn 100
string SelfString(string str)
{
int i, a[10]={0};
string s;
for(i=0; i<str.length(); i++)
a[str[i]-'0']++;
for(i=0; i<10; i++)
{
if(a[i])
{
while(a[i])
{
s += a[i]%10+'0';
a[i] /= 10;
}
s += i+'0';
}
}
return s;
}
int main()
{
string str;
while(cin >> str, str != "-1")
{
string a[maxn];
int i, j, k=1, ans=0;
a[0] = str;
for(i=1; i<=15; i++)
{
a[i] = SelfString(a[i-1]);
if(a[i] == a[i-1])
{
ans = 1;
break;
}
for(j=0; j<i; j++)
if(a[i] == a[j])
{
ans = 2;
break;
}
if(j < i)
break;
}
if(ans==1 && i==1)
cout << a[0] <<" is self-inventorying "<<endl;
else if(ans==1)
cout << a[0] <<" is self-inventorying after "<< i-1 <<" steps "<<endl;
else if(i <= 15)
cout << a[0] <<" enters an inventory loop of length " << i-j <<endl;
else
cout << a[0] << " can not be classified after 15 iterations" <<endl;
}
return 0;
#include<string>
using namespace std;
#define maxn 100
string SelfString(string str)
{
int i, a[10]={0};
string s;
for(i=0; i<str.length(); i++)
a[str[i]-'0']++;
for(i=0; i<10; i++)
{
if(a[i])
{
while(a[i])
{
s += a[i]%10+'0';
a[i] /= 10;
}
s += i+'0';
}
}
return s;
}
int main()
{
string str;
while(cin >> str, str != "-1")
{
string a[maxn];
int i, j, k=1, ans=0;
a[0] = str;
for(i=1; i<=15; i++)
{
a[i] = SelfString(a[i-1]);
if(a[i] == a[i-1])
{
ans = 1;
break;
}
for(j=0; j<i; j++)
if(a[i] == a[j])
{
ans = 2;
break;
}
if(j < i)
break;
}
if(ans==1 && i==1)
cout << a[0] <<" is self-inventorying "<<endl;
else if(ans==1)
cout << a[0] <<" is self-inventorying after "<< i-1 <<" steps "<<endl;
else if(i <= 15)
cout << a[0] <<" enters an inventory loop of length " << i-j <<endl;
else
cout << a[0] << " can not be classified after 15 iterations" <<endl;
}
return 0;
}