這是一道簡單的模拟題,統計字元串中1,2....9的個數,同時組成一個新的字元串,并和最初的比較,有四種情況~~
具體代碼如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char word[100],str[20][100],tempstr[100];
int count[10];
int index;
void change(int x)
{
memset(count,0,sizeof(count));
int len,i,j;
len = strlen(str[x]);
for(i = 0;i < len;i++)
count[str[x][i]-'0']++;
index = 0;
for(i = 0;i < 10;i++){
if(count[i]){
if(count[i] > 9){
tempstr[index++] = count[i]/10 + '0';
tempstr[index++] = count[i]%10 + '0';
}else{
tempstr[index++] = count[i] + '0';
}
tempstr[index++] = i + '0';
}
}
tempstr[index] = 0;
}
int main()
{
int i,j;
while(gets(str[0])){
if(str[0][0]=='-') break;
for(i = 1;i <= 15;i++){
change(i-1);
for(j = i-1;j>=0;j--){
if(!memcmp(tempstr,str[j],strlen(str[j])>strlen(tempstr)?strlen(str[j]):strlen(tempstr)))
{
if(i == 1) {
printf("%s is self-inventorying\n",str[0]);
}
else if(j == i-1) {
printf("%s is self-inventorying after %d steps \n",str[0],i-1);
}
else {
printf("%s enters an inventory loop of length %d\n",str[0],i - j);
}
goto mark;
}
}
strcpy(str[i],tempstr);
}
printf("%s can not be classified after 15 iterations\n",str[0]);
mark:;
}
return 0;
}