问题描述
Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a.
If there is no answer or the answer is not fit in 32-bit signed integer, then return 0.
样例
Given a = 48, return 68.
Given a = 15, return 35.
分析
计算a的所有因子(10以下的),然后把因子从小到大排列成一个整数即是结果。如果a含有大于7的质因数,例如11,这样的数将不会被除尽,连续被除之后不为1
代码
public class Solution {
/**
* @param a: a positive integer
* @return: the smallest positive integer whose multiplication of each digit equals to a
*/
public int smallestFactorization(int a) {
if(a < 10) {
return a;
}
long result = 0;
long base = 1;
for(int i = 9; i > 1; i--) {
while (a % i == 0) {
result += i * base;
base *= 10;
a /= i;
}
}
if(a == 1 && result <= Integer.MAX_VALUE) {
return (int)result;
}
return 0;
}
}