zoukankan      html  css  js  c++  java
  • Numbers That Count POJ

    "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 <iostream>
    #include <cstdio>
    #include <cstring>
    #define maxn 85
    using namespace std;
    
    char s[maxn];
    int a[10];
    
    int main()
    {
        while(1)
        {
            char str[16][maxn]= {0}; //先全部变换,将原始数字和变换后的都保存下来
            memset(s,0,sizeof(s)); //初始化
            scanf("%s",s);
            if(s[0]=='-')
                break;
            strcpy(str[0],s);
            for(int i=0; i<15; i++) //15次变换
            {
                memset(a,0,sizeof(a)); //初始化
                for(int j=0; j<10; j++) //查找0~9每个数字,并保存至数字a[j]
                    for(int k=0; k<strlen(str[i]); k++)
                    {
                        if(str[i][k]==j+'0')
                            a[j]++;
                    }
                for(int j=0,k=0; j<10; j++)
                    if(a[j]>=10) //这里的细节需要注意一下,个数大于或等于10,需要保存三位数
                    {
                        str[i+1][k]=a[j]/10+'0';
                        str[i+1][k+1]=a[j]%10+'0';
                        str[i+1][k+2]=j+'0';
                        k+=3;
                    }
                    else if(a[j]>0 && a[j]<10)
                    {
                        str[i+1][k]=a[j]+'0';
                        str[i+1][k+1]=j+'0';
                        k+=2;
                    }
            }
            bool flag=true;
            if(strcmp(str[0],str[1])==0)
            {
                printf("%s is self-inventorying
    ",str[0]);
                flag=false;
            }
            if(flag)
                for(int i=1; i<=15; i++)
                    if(strcmp(str[i],str[i+1])==0)
                    {
                        printf("%s is self-inventorying after %d steps
    ",str[0],i);
                        flag=false;
                        break;
                    }
            if(flag)
                for(int i=13; i>=0; i--)
                    if(strcmp(str[15],str[i])==0)
                    {
                        printf("%s enters an inventory loop of length %d
    ",str[0],15-i);
                        flag=false;
                        break;
                    }
            if(flag)
                printf("%s can not be classified after 15 iterations
    ",str[0]);
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    参考SQLHelper编写的OracleHelper
    微软原版SQLHelper类
    AppFabric 版本区分
    ASP.NET (HttpModule,HttpHandler)
    ASP.NET内部原理(HttpHandler和HttpModule)
    IIS 7.0, ASP.NET, pipelines, modules, handlers, and preconditions
    理解I/O Completion Port
    asp.net mvc跨域filter
    c#生成MD5字符串
    生成格式化的json
  • 原文地址:https://www.cnblogs.com/l609929321/p/6895041.html
Copyright © 2011-2022 走看看