题目链接:http://acm.jlu.edu.cn/joj/showproblem.php?pid=1379
解题心得:
1,基本的语法不要弄错,当时做的时候,memset用错了,错因是把第二个参数传为数组的大小,第三个参数传为要设的值了,实际应该是参数二为要设的值,参数三为数组大小,并且为整型数组赋值时第二个参数只有为0或-1时才能达到预期效果。
2,在用标准库中的集合和数组时,一定要考虑清楚什么时候该清空和什么时候该设为空,否则就出现怎么都改不对的错误,我就是困在这儿的!!!
3,用visit数组统计各位数字的个数自认为是个挺好的想法,可以看作是思想的进步
-
#include <stdio.h>
-
#include <string.h>
-
-
char input [ 81 ];
-
char result [ 15 ] [ 81 ];
-
int visit [ 10 ];
-
-
void SetResult ( int i )
-
{
-
int j,k,len;
-
memset (visit, 0, sizeof (visit ) );
-
if (i == 0 )
-
{
-
len = strlen (input );
-
for (j = 0;j < len;j++ )
-
visit [input [j ] - '0' ]++;
-
}
-
else
-
{
-
len = strlen (result [i - 1 ] );
-
for (j = 0;j < len;j++ )
-
visit [result [i - 1 ] [j ] - '0' ]++;
-
}
-
-
k = 0;
-
for (j = 0;j < 10;j++ )
-
{
-
if (visit [j ] >= 10 )
-
{
-
int a = visit [j ] / 10;
-
int b = visit [j ] % 10;
-
result [i ] [k ] = a + '0';
-
result [i ] [k + 1 ] = b + '0';
-
result [i ] [k + 2 ] = j + '0';
-
k += 3;
-
}
-
else if (visit [j ] > 0 )
-
{
-
result [i ] [k ] = visit [j ] + '0';
-
result [i ] [k + 1 ] = j + '0';
-
k += 2;
-
}
-
}
-
}
-
-
bool Find ( int i,int& first )
-
{
-
if ( strcmp (result [i ],input ) == 0 )
-
{
-
first = -1;
-
return true;
-
}
-
-
int j;
-
for (j = 0;j < i;j++ )
-
{
-
if ( strcmp (result [j ],result [i ] ) == 0 )
-
{
-
first = j;
-
return true;
-
}
-
}
-
return false;
-
}
-
-
int main ( int argc, char* argv [ ] )
-
{
-
int b_num;
-
while ( scanf ( "%d",&b_num ) != EOF )
-
{
-
while (b_num-- )
-
{
-
while ( scanf ( "%s",input ) && strcmp (input, "-1" ) != 0 )
-
{
-
int i;
-
bool find = false;
-
memset (result, 0, sizeof (result ));//每次处理一个字符串都应该将result设为空
-
for (i = 0;i < 15;i++ )
-
{
-
SetResult (i );
-
if (i == 0 )
-
{
-
if ( strcmp (result [i ],input ) == 0 )
-
{
-
printf ( "%s is self-inventorying/n",input );
-
find = true;
-
break;
-
}
-
}
-
else
-
{
-
int first;
-
if (Find (i,first ) )
-
{
-
if (first == i - 1 )
-
printf ( "%s is self-inventorying after %d steps/n",input,i );
-
else
-
printf ( "%s enters an inventory loop of length %d/n",input,
-
i - first );
-
find = true;
-
break;
-
}
-
}
-
}
-
if (!find )
-
printf ( "%s can not be classified after 15 iterations/n",input );
-
}
-
printf ( "/n" );
-
}
-
}
-
return 0;
-
}