Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

确实要考虑数字的个数大于十的情况啊。(附代码)

Posted by ruanshaomin at 2009-07-23 09:59:30 on Problem 1016
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	char str[81],ch[81],str1[81],sh[17][81];
	int i,j,leng,b[10],count,flag;;
	while(1)
	{
		for(i=0;i<17;i++)
		for(j=0;j<81;j++)
		sh[i][j]='\0';
		for(i=0;i<81;i++)
		ch[i]='\0';
		memset(b,0,sizeof(b));
		scanf("%s",str);
		strcpy(str1,str);
		if(strcmp(str,"-1")==0)
		break;
		strcpy(sh[0],str);
		for(count=1;count<=15;count++)
		{
			memset(b,0,sizeof(b));
			leng=strlen(str);
			for(i=0;i<leng;i++)
			b[str[i]-'0']++;
			j=0;
			for(i=0;i<10;i++)
			{
				if(b[i]!=0)
				{
					if(b[i]>9)
					{
						ch[j++]=b[i]/10+'0';
						ch[j++]=b[i]%10+'0';
						ch[j]=i+'0';
					}
					else
					{
						ch[j++]=b[i]+'0';
						ch[j]=i+'0';
					}
					j++;
				}
			}
			
			strcpy(sh[count],ch);
			if(strcmp(ch,str)==0)
			{
				if(count==1)
				printf("%s is self-inventorying\n",str1);
				else
				printf("%s is self-inventorying after %d steps\n",str1,count-1);
				break;
			}
			flag=0;
			for(i=0;i<count;i++)
			{
				
				if(strcmp(ch,sh[i])==0)
				{
					printf("%s enters an inventory loop of length %d\n",str1,count-i);
					flag=1;
					break;
				}
			}
			if(flag==1)
			break;
			
			strcpy(str,ch);
			for(i=0;i<81;i++)
			ch[i]='\0';
		}
		if(count==16)
		printf("%s can not be classified after 15 iterations\n",str1); 
	}
	
}
		
		

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator