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
大致题意:如49,找到最小数字(77)满足7*7=49
768,2*6*8*8=768
题解:
高精度被除整数➗整数(个位)
位数为1的整数特判
从大到小寻找因子,使得下一次的被除数尽量小
除法模拟过程中不断进行char*与数字的转换,末尾'\0'
商字符串中含有前导0,记得去掉
当商的位数>1,找不到符合条件的数
#include<iostream>
using namespace std;
const int N = 1010;
char s[N], ans[N];//商
int num[3 * N];
bool iscount(int i) {
int mod = 0, k = 0;
char* q;
for (int j = 0; s[j] != '\0'; j++) {//模拟除法
mod = mod * 10 + s[j] - '0';//计算商的当前位,送入ans
printf("mod:%d\n", mod);
ans[k++] = mod / i + '0';
mod %= i;
}ans[k] = '\0';
if (mod != 0)return 0;//最后余数非零,不能整除
q = ans;
while (*q == '0')q++;//去掉q前导的无用0
int j;//复制更新被除数s
for (j = 0; *q != '\0'; j++, q++)s[j] = *q;
s[j] = '\0';
return 1;
}
int main() {
while (scanf("%s", s), s[0] != '-') {
if (s[1] == '\0') {
printf("1%s\n", s);
continue;
}int j = 0;
for (int i = 9; i > 1; i--) {
while (iscount(i)) {//是否整除
printf("start:%d\n",i);
num[++j] = i;
}
}
//商的位数大于一
if (strlen(s) > 1) {
printf("There is no such number.\n");
continue;
}
//倒序输出
while (j > 0)printf("%d", num[j--]);
printf("\n");
}
return 0;
}