/*____________________________________________POJ 1016题________________________________________________ Numbers That Count Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7578 Accepted: 2560 Description: "Kronecker's Knumbers" is a little company that manufactures plastic digits for use in signs (theater marquees, gas station price displays, and so on). The owner and sole employee, Klyde Kronecker, keeps track of how many digits of each type he has used by maintaining an inventory book. For instance, if he has just made a sign containing the telephone number "5553141", he'll write down the number "5553141" in one column of his book, and in the next column he'll list how many of each digit he used: two 1s, one 3, one 4, and three 5s. (Digits that don't get used don't appear in the inventory.) He writes the inventory in condensed form, like this: "21131435". The other day, Klyde filled an order for the number 31123314 and was amazed to discover that the inventory of this number is the same as the number---it has three 1s, one 2, three 3s, and one 4! He calls this an example of a "self-inventorying number", and now he wants to find out which numbers are self-inventorying, or lead to a self-inventorying number through iterated application of the inventorying operation described below. You have been hired to help him in his investigations. Given any non-negative integer n, its inventory is another integer consisting of a concatenation of integers c1 d1 c2 d2 ... ck dk , where each ci and di is an unsigned integer, every ci is positive, the di satisfy 0<=d1<d2<...<dk<=9, and, for each digit d that appears anywhere in n, d equals di for some i and d occurs exactly ci times in the decimal representation of n. For instance, to compute the inventory of 5553141 we set c1 = 2, d1 = 1, c2 = 1, d2 = 3, etc., giving 21131435. The number 1000000000000 has inventory 12011 ("twelve 0s, one 1"). An integer n is called self-inventorying if n equals its inventory. It is called self-inventorying after j steps (j>=1) if j is the smallest number such that the value of the j-th iterative application of the inventory function is self-inventorying. For instance, 21221314 is self-inventorying after 2 steps, since the inventory of 21221314 is 31321314, the inventory of 31321314 is 31123314, and 31123314 is self-inventorying. Finally, n enters an inventory loop of length k (k>=2) if k is the smallest number such that for some integer j (j>=0), the value of the j-th iterative application of the inventory function is the same as the value of the (j + k)-th iterative application. For instance, 314213241519 enters an inventory loop of length 2, since the inventory of 314213241519 is 412223241519 and the inventory of 412223241519 is 314213241519, the original number (we have j = 0 in this case). Write a program that will read a sequence of non-negative integers and, for each input value, state whether it is self-inventorying, self-inventorying after j steps, enters an inventory loop of length k, or has none of these properties after 15 iterative applications of the inventory function. Input: A sequence of non-negative integers, each having at most 80 digits, followed by the terminating value -1. There are no extra leading zeros. Output: For each non-negative input value n, output the appropriate choice from among the following messages (where n is the input value, j is a positive integer, and k is a positive integer greater than 1): n is self-inventorying n is self-inventorying after j steps n enters an inventory loop of length k n can not be classified after 15 iterations Sample Input: 22 31123314 314213241519 21221314 111222234459 -1 Sample Output: 22 is self-inventorying 31123314 is self-inventorying 314213241519 enters an inventory loop of length 2 21221314 is self-inventorying after 2 steps 111222234459 enters an inventory loop of length 2 ______________________________________________________________________________________________________*/ #include<stdio.h> #include<string.h> void Sort(char p[],int n) //冒泡排序 { int i,j,flag,temp; for(i=1;i<n;i++) { flag=0; for(j=0;j<n-i;j++) if(p[j]>p[j+1]) { flag=1; temp=p[j]; p[j]=p[j+1]; p[j+1]=temp; } if(flag==0) break; } } //对串p[]进行编码,输出到p2[]中 void Convert(char p[],char p2[],int n1) { int i,j,count=1; char p1[100]={'/0'}; strcpy(p1,p); Sort(p1,n1); // printf("%s/n",p1); for(i=0,j=0;i<n1;i++) { if( p1[i]!=p1[i+1] || i==n1-1 ) //前后不相同或i是倒数第二个字符时,停止计数,写入p2[] { if(count<10) p2[j++]=count+'0'; else { p2[j++]=count/10+'0'; p2[j++]=count%10+'0'; } p2[j++]=p1[i]; count=1; } else count++; } } //判断是否进入某个长度的循环 int Judgeloop(char p[][100],char *p2,int loop) { for(int i=0;i<loop;i++) if( strcmp(p[i],p2) ==0 ) { return loop-i-1; break; } return 0; //record[][]中没有和dest[]相同的 } int main() { int count,num,loop; char source[100]={'/0'},dest[100]={'/0'},last[100]={'/0'}; char record[20][100]={'/0'}; //FILE *fin=fopen("input.txt","r"); //fscanf(fin,"%s",source); scanf("%s",source); while(1) { if(strcmp(source,"-1")==0) break; count=0; loop=0; num=0; strcpy(last,source); strcpy(record[num++],source); while(1) { Convert(last,dest,strlen(last)); //对串last编码出其inventory值,放在dest中,last保持不变 strcpy(record[num++],dest); if(num>16) //还是不够仔细,num==16时,正好是进行15次,WA那次因为写成了num>=16 { printf("%s can not be classified after 15 iterations/n",source); break; } if( strcmp(source,dest)==0 && count==0) { printf("%s is self-inventorying/n",source); break; }else if( strcmp(last,dest)==0) { printf("%s is self-inventorying after %d steps/n",source,count); break; }else if( loop=Judgeloop(record,dest,num) ) //loop值非零 { printf("%s enters an inventory loop of length %d/n",source,loop); break; }else { memset(last,'/0',sizeof(last)); strcpy(last,dest); memset(dest,'/0',sizeof(last)); count++; } } memset(source,'/0',sizeof(source)); memset(dest,'/0',sizeof(dest)); memset(last,'/0',sizeof(last)); memset(record,'/0',sizeof(record)); //fscanf(fin,"%s",source); scanf("%s",source); } return 0; } /******************************************************************************** (1)注意字符数组的清空,要不然上个测试用例留下的残骸可能会影响下个结果 (2)注意审题,第一次WA因为没注意到可以是15次操作后出结果 ********************************************************************************/