zoukankan      html  css  js  c++  java
  • 【练习赛2补题】poj 2325 Persistent Numbers 【高精度除法+贪心】

    Description

    The multiplicative persistence of a number is defined by Neil Sloane (Neil J.A. Sloane in The Persistence of a Number published in Journal of Recreational Mathematics 6, 1973, pp. 97-98., 1973) as the number of steps to reach a one-digit number when repeatedly multiplying the digits. Example: 
    679 -> 378 -> 168 -> 48 -> 32 -> 6.

    That is, the persistence of 679 is 6. The persistence of a single digit number is 0. At the time of this writing it is known that there are numbers with the persistence of 11. It is not known whether there are numbers with the persistence of 12 but it is known that if they exists then the smallest of them would have more than 3000 digits. 
    The problem that you are to solve here is: what is the smallest number such that the first step of computing its persistence results in the given number?

    Input

    For each test case there is a single line of input containing a decimal number with up to 1000 digits. A line containing -1 follows the last test case.

    Output

    For each test case you are to output one line containing one integer number satisfying the condition stated above or a statement saying that there is no such number in the format shown below.

    Sample Input

    0
    1
    4
    7
    18
    49
    51
    768
    -1
    

    Sample Output

    10
    11
    14
    17
    29
    77
    There is no such number.
    2688

    题意:找到一个数,能够使这个数各个位连续相乘得到题目所给的个位数,输入的数为一个个位数时,直接输出1%s.比如,题目所给的679->6,6*7*9->378,3*7*8->168,1*6*8->48,4*8->32,3*2->6
    思路:由于输入的数最多可以达到1000位,所以我们以字符串形式读入,将这个大整数从高位到低位依次对9~2的数i整除,如果能够整除i,则将i存入一个数组范围为3000+的数组,并且将大整数整除i后的商
    作为新的被除数进行运算,最后逆序输出这个数组,实际上实现划线部分的步骤,就是模拟大整数除法。
    难点:对于我来说,题意比较难get
    #include<stdio.h>
    #include<string.h>
    #define N 1010
    char str[N],ans[N];
    int num[3*N];
    int count(int i)
    {
        int j,mod=0,k=0;
        char *q;
        for(j = 0;str[j]!=''; j ++)//模拟除法运算过程 
        {
            mod = mod*10+str[j]-'0';
            ans[k++] = mod/i +'0';
            mod%=i;
        }
        ans[k] = '';
        q = ans;
        while(*q=='0')//去掉前导0 
            q++;
        if(mod!=0)//如果全部都是0,则说明不能整除 
            return 0;
        for(j = 0; *q!='';j++,q++) //重新将商作为被除数 
            str[j] = *q;
        str[j] = '';
        return 1; 
    }
    
    int main()
    {
        int i,j;
        while(scanf("%s",str),str[0]!='-')
        {
            j=0;
            if(str[1]=='') //输入为一个数字的时候 
            {
                printf("1%s
    ",str);
                continue;
            }
            for(i = 9; i > 1; i --)
                while(count(i))//判断能否满足整除 
                {
                    num[j++] = i;
                }
            if(strlen(str)>1)//如果不能被除尽 
            {
                printf("There is no such number.
    ");
                continue;
            }
            while(j>0)//逆序输出字符序号 
                printf("%d",num[--j]);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    python文件处理-读、写
    Python三元运算和lambda表达式
    可变、不可变数据类型和hash
    Python常见字符编码间的转换
    Python常用字符编码
    spring cloud 使用spring cloud bus自动刷新配置
    spring cloud config--client
    spring cloud Config--server
    git 小乌龟安装教程
    spring cloud Eureka常见问题总结
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7392462.html
Copyright © 2011-2022 走看看