1016.c

来自「pku online judge的一些题目的代码,全部AC的,适合编程初学者以及」· C语言 代码 · 共 64 行

C
64
字号
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp( const void *a , const void *b ) 
{ 
	return *(char *)a - *(char *)b; 
} 
int main()
{
	char s[81],a[81],b[30][81];
	int i,j,k,m,count,time;
	while(scanf("%s",s),strcmp(s,"-1"))
	{
		time=0;
		while(1)
		{
			for(k=0;k<time;k++)
				if(strcmp(s,b[k])==0)
					break;
			if(k<time)
				break;
			strcpy(b[time],s);
			m=strlen(s);
			qsort(s,m,sizeof(s[0]),cmp);
			for(i=j=0,count=1;i<m;i++)
				if(s[i]==s[i+1])
					count++;
				else
				{
					if(count>9)
					{
						a[j++]=count/10+'0';
						a[j++]=count%10+'0';
						a[j++]=s[i];
					}
					else
					{
						a[j++]=count+'0';
						a[j++]=s[i];
					}
					count=1;
				}
			a[j]='\0';
			if(strcmp(b[time],a))
			{
				strcpy(s,a);
				time++;
				if(time==16)
					break;
			}
			else
				break;
		}
		if(time==16)
			printf("%s can not be classified after 15 iterations\n",b[0]);
		else if(k<time)
			printf("%s enters an inventory loop of length %d\n",b[0],time-k);
		else if(time==0)
			printf("%s is self-inventorying\n",b[0]);
		else
			printf("%s is self-inventorying after %d steps\n",b[0],time);
	}
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?