poj1016-Numbers That Count

模拟题poj1016-Numbers That Count

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 

模拟题一定要有清晰的思路,不要轻易的开始敲代码,仔细分析题目。最好在做之前先用自己的方法把解题的大致结构手写下来


大致题意

题意不难懂,对于任意的数字串n,都可以压缩存储为

c1 d1 c2 d2 .... ck dk 形式的数字串

而存在一些特别的数字串,其压缩前后的样子是一模一样的

定义这种数字串为self-inventorying

 

当我们把n看成原串,

A为n压缩1次后的数字串,

B为n压缩2次后的数字串(即A压缩1次后的数字串)

....以此类推

K为n压缩k次后的数字串(即K-1压缩k-1次后的数字串)

 

则可以延伸出数字串n的3种属性:

1、  n压缩1次就马上出现self-inventorying特性,即 n n n n n n n .....

2、  n压缩j次后的数字串J出现self-inventorying特性,即 n A B C....H I J J J J J J J

3、  n压缩j次后的数字串J,每再压缩K次,重新出现数字串J,即n A B... J ..K J ..K J..K J

其中K称为循环间隔,K>=2

现给定一字符串,输出其属性。  属性1优于属性2,属性2优于属性3;

当且仅当n的3种属性都不存在时,n can not be classified after 15 iterations

 

解题思路

字符串处理,纯粹的模拟题

压缩n时要注意,ck可能是1位,也可能是2位,需要判断。

 

设R(n)为描述整数n的压缩数字串

#include<stdio.h>
#include<string.h>
/*该函数用于压缩字符串n,并把压缩后的结果给t*/
void R(char *n,char *t)
{
    int i,j;
    int time[10]={0};//记录0~9每个数字出现的次数
    for(i=0;i<strlen(n);i++)
        time[n[i]-'0']++;
    for(i=0,j=0;i<10;i++)
    {
        if(time[i])
        {
            /*因为n最长只有80个字符,所以time最大不超过两位数,所以分两种情况*/
            if(time[i]>0&&time[i]<=9)
            {
                t[j++]=time[i]+'0';
                t[j++]=i+'0';
            }
            if(time[i]>=10)
            {
                t[j++]=time[i]/10+'0';
                t[j++]=time[i]%10+'0';
                t[j++]=i+'0';
            }
        }
    }
    t[j]='\0';
}

int main()
{
    char n[16][100];
    int i,j;
    while(scanf("%s",n[0])!=EOF)
    {
        if(strcmp(n[0],"-1")==0) break;//n[0]就是原字符串
        int flag1=0;    //属性1,n is self-inventorying
        int flag2=0;    //属性2,n is self-inventorying after j steps,顺便记录j
        int flag3=0;    //属性3,n is enters an inventory loop of length k,顺便记录k
        for(i=1;i<=15;i++)  //把数字串n[0]压缩15次
            R(n[i-1],n[i]);
        if(strcmp(n[0],n[1])==0)  //属性1判断,n压缩1次就是其本身
            flag1=1;
        if(flag1==0)
        {
            for(j=1;j<15;j++)
            {
                if(strcmp(n[j],n[j+1])==0)  //属性2判断,n压缩j次后的数字串n[j]具有属性1
                {
                    flag2=j;
                    break;
                }
            }
            if(flag2==0)
			{
				for(j=1;j<=15;j++)  //属性3判断,两两枚举各次压缩的数字串,注意循环间隔>=2
				{
					for(i=0;i<=j-2;i++)
					{
						if(strcmp(n[j],n[i])==0)
						{
							flag3=j-i;
							break;
						}
					}
					if(flag3!=0)
						break;
				}
			}
        }
        printf("%s",n[0]);
        if(flag1!=0)
            printf(" is self-inventorying\n");
        else if(flag2!=0)
            printf(" is self-inventorying after %d steps\n",flag2);
        else if(flag3!=0)
            printf(" enters an inventory loop of length %d\n",flag3);
        else
            printf(" can not be classified after 15 iterations\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值