pku 1016 总是wrong answer,高手们来看看
题目大意:就是统计0-9的个数,然后将它们按顺序组成一个新的字符串。比如5553141,有3个5,1一个3,2个1,1个4.按顺序排列后就是 21131435。 一次统计称为一次迭代。最多迭代15次。最后会的出4种结果。
1.第一次迭代得到的结果和源字符串相等,这时输出n is self-inventorying 。
2.从某次迭代开始得到结果不再变化这时输出n is self-inventorying after j steps 。 j为到达不变化的字符串时迭代的次数。
3.以上两种情况之外若得到的字符串与前面某个字符串相同,则进入循环,输出n enters an inventory loop of length k 。k是这两个相同的串之间的距离。
4.若以上3种情况均不满足, 输出n can not be classified after 15 iterations。
将每次输入的字符串迭代15次,存入字符串数组str[],然后比较。
1. 若str[0]==str[1] 则满足上述第一种情况。
2. 1-15循环,若从某个位置开始,一个串与它前面一个串相等,则满足上述第2种情况。
3. 从0-14开始对每个i调用find_loop函数, 函数中从i+1开始对j进行自增, 若str[j]==str[i],则满足上述第3种情况。k=j-i.
4. 以上三种均不满足,均按第4种处理。
输入-1时结束
我的代码:
#include<stdio.h>
#include<string.h>
char arr[85];
int compute(char data[])
{
int i = 0, j, len, k = 0, count[10];
char temp[16][85], arr[85];
strcpy(arr, data);
strcpy(temp[0], arr);
for(k = 0; k < 15; k++)
{
for(i = 0; i < 10; i++)count[i] = 0;
len = strlen(temp[k]);
for(i = 0; i < len; i++)
{
switch(temp[k][i])
{
case '0':count[0]++;break;
case '1':count[1]++;break;
case '2':count[2]++;break;
case '3':count[3]++;break;
case '4':count[4]++;break;
case '5':count[5]++;break;
case '6':count[6]++;break;
case '7':count[7]++;break;
case '8':count[8]++;break;
case '9':count[9]++;break;
default:break;
}//switch
}//统计
for(i = 0, j = 0; i < 10; i++)
{
if(count[i] == 0)continue;
temp[k + 1][j] = count[i] + '0';//printf("a: %c ", temp[k + 1][j]);
temp[k + 1][j + 1] = i + '0';//printf("b: %c \n", temp[k + 1][j + 1]);
j = j+ 2;
}//放人temp[k + 1]
temp[k + 1][j] = '\0';//printf("temp[k + 1]: %s\n", k + 1, temp[k + 1]);
//printf("temp[%d]: %s hh\n", k + 1, temp[k + 1]);
for(i = 0; i <= k; i++)
{
if(strcmp(temp[k + 1], temp[i]) == 0 && k == 0)
{
printf("%s is self-inventorying\n", arr);
return 0;
}//第一种情况
else if(strcmp(temp[k + 1], temp[i]) == 0 && k > 0 && k == i)
{
printf("%s is self-inventorying after %d steps\n", arr, k);
return 0;
}//第二种情况
else if(strcmp(temp[k + 1], temp[i]) == 0)
{
printf("%s enters an inventory loop of length %d\n", arr, k + 1 - i);
return 0;
}//第三种情况
}//for
}//k
printf("%s can not be classified after 15 iterations\n", arr);//第四种情况
return 0;
}
int main()
{
while(1)
{
scanf("%s", &arr);
if(arr[0] == '-')break;
compute(arr);
}
return 0;
}
测试了很多数距都对了,但就是wrong answer,不知哪里错了,高手们帮帮忙啊