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
#include<stdio.h>
#include<string.h>
char m[1010];
char str[1010];
int Num[1010];
int num(int N)
{
int c = 0, i;
for (i = 0; str[i] != '\0'; i++)//模拟大数除法
{
c = c * 10 + str[i] - '0';
m[i] = c / N + '0';
c = c % N;
}
if (c)//如果最后不能除尽
return 0;
else
{
m[i] = '\0';//结束该字符串,若不加在后面复制字符串的时候可能会运行错误
i = 0;
while (m[i] == '0')
{
i++;
}
strcpy(str, m + i);//将除后的商再赋值到字符串中,再次计算
return 1;
}
}
int main()
{
while (scanf(" %s", str))
{
if (str[0] == '-')//为-1的时候结束
break;
if (str[1] == '\0')//如果是个位数直接输出 '1s';
{
printf("1%s\n", str);
continue;
}
else
{
int j = 0;
for (int i = 9; i >= 2; i--)//因为是最小连续数,所以首先要位数最小,而每位数越大,乘积就越大,位数就越小,而1的乘积没用所以从9循环到2
{
if (num(i))//如果可以除尽
{
Num[j++] = i;//添加一位
i++;//因为下次除尽的可能还是这个数,如果可以除尽就再用一次这个数
}
if (strlen(str) == 1 && str[0] == '1')//当该数被除尽时结束
break;
}
if (strlen(str) > 1)//如果不能被除尽
printf("There is no such number.\n");
else
{
j = j - 1;
for (; j >= 0; j--)//逆序输出所求的数
printf("%d", Num[j]);
printf("\n");
}
}
}
}