码迷,mamicode.com
首页 > 其他好文 > 详细

【练习赛2补题】poj 2325 Persistent Numbers 【高精度除法+贪心】

时间:2017-08-18 23:45:55      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:ext   eps   single   运算   pst   printf   code   ini   nal   

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]!=\0; j ++)//模拟除法运算过程 
    {
        mod = mod*10+str[j]-0;
        ans[k++] = mod/i +0;
        mod%=i;
    }
    ans[k] = \0;
    q = ans;
    while(*q==0)//去掉前导0 
        q++;
    if(mod!=0)//如果全部都是0,则说明不能整除 
        return 0;
    for(j = 0; *q!=\0;j++,q++) //重新将商作为被除数 
        str[j] = *q;
    str[j] = \0;
    return 1; 
}

int main()
{
    int i,j;
    while(scanf("%s",str),str[0]!=-)
    {
        j=0;
        if(str[1]==\0) //输入为一个数字的时候 
        {
            printf("1%s\n",str);
            continue;
        }
        for(i = 9; i > 1; i --)
            while(count(i))//判断能否满足整除 
            {
                num[j++] = i;
            }
        if(strlen(str)>1)//如果不能被除尽 
        {
            printf("There is no such number.\n");
            continue;
        }
        while(j>0)//逆序输出字符序号 
            printf("%d",num[--j]);
        printf("\n");
    }
    return 0;
}

 

【练习赛2补题】poj 2325 Persistent Numbers 【高精度除法+贪心】

标签:ext   eps   single   运算   pst   printf   code   ini   nal   

原文地址:http://www.cnblogs.com/chengdongni/p/7392462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!