Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ‘n’. The result ‘p’ should have minimum two digits.

Examples:

Input:  n = 36
Output: p = 49
// Note that 4*9 = 36 and 49 is the smallest such number Input: n = 100
Output: p = 455
// Note that 4*5*5 = 100 and 455 is the smallest such number Input: n = 1
Output:p = 11
// Note that 1*1 = 1 Input: n = 13
Output: Not Possible

For a given n, following are the two cases to be considered.
Case 1: n < 10 When n is smaller than n, the output is always n+10. For example for n = 7, output is 17. For n = 9, output is 19.

Case 2: n >= 10 Find all factors of n which are between 2 and 9 (both inclusive). The idea is to start searching from 9 so that the number of digits in result are minimized. For example 9 is preferred over 33 and 8 is preferred over 24.
Store all found factors in an array. The array would contain digits in non-increasing order, so finally print the array in reverse order.

     public static int findSmallest(int n){
if(n < 10) return 10 + n;
int out = 0;
int pos = 0;
for(int i = 9; i > 1; i --){
while(n % i == 0){
n /= i;
out += i * Math.pow(10, pos);
pos ++;
}
}
return n ==1 ? out : -1;
}

Find the smallest number whose digits multiply to a given number n的更多相关文章

  1. 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)

    We define the smallest positive real number as the number which is explicitly greater than zero and ...

  2. AtCoder Regular Contest 090 F - Number of Digits

    题目链接 Description For a positive integer \(n\), let us define \(f(n)\) as the number of digits in bas ...

  3. 【leetcode】1295. Find Numbers with Even Number of Digits

    题目如下: Given an array nums of integers, return how many of them contain an even number of digits. Exa ...

  4. ExtJS学习-----------Ext.Number,ExtJS对javascript中的Number的扩展

    关于ExtJS对javascript中的Number的扩展,能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 以 ...

  5. 'Invalid update: invalid number of rows in section xx. The number of rows contained in an existing section after the update (xxx)...

    'Invalid update: invalid number of rows in section 5.  The number of rows contained in an existing s ...

  6. leetcode 402. Remove K Digits 、321. Create Maximum Number

    402. Remove K Digits https://www.cnblogs.com/grandyang/p/5883736.html https://blog.csdn.net/fuxuemin ...

  7. [AtCoder arc090F]Number of Digits

    Description 题库链接 记 \(d\) 在十进制下的位数为 \(f(d)\) .给出询问 \(S\) ,求有多少对 \((l,r)\) 使得 \[\sum_{i=l}^r f(i)=S\] ...

  8. 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)

    原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ from:ht ...

  9. leetcode 374. Guess Number Higher or Lower 、375. Guess Number Higher or Lower II

    374. Guess Number Higher or Lower 二分查找就好 // Forward declaration of guess API. // @param num, your gu ...

随机推荐

  1. JTable的DefaultModel方法getValueAt(a,row)

    行和列都是从0开始索引的,而且不包括netbeans生成的表格头,而是从数据开始,否则就会报错

  2. 多点触摸画板(MultiTouchCanvas)

    这是个简单的支持多点触摸的画板控件, 绘制功能基于WPF InkCanvas,也是我drawTool系列文章的开篇. 阅读该文章后可能产生一些问题: 1. 如果对生成的笔迹对象进行控制 如果要对生成的 ...

  3. C 实现一个简易的Http服务器

    引言 做一个老实人挺好的,至少还觉得自己挺老实的. 再分享一首 自己喜欢的诗人的一首 情景诗. 每个人总会有问题,至少喜欢就好, 本文 参照 http 协议   http://www.cnblogs. ...

  4. [转]C++编写Config类读取配置文件

    //Config.h #pragma once #include <string> #include <map> #include <iostream> #incl ...

  5. linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符)

    Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式: var q = from p in ...

  6. C# ArrayList的用法总结

    C# ArrayList的用法总结 System.Collections.ArrayList类是一个特殊的数组.通过添加和删除元素,就可以动态改变数组的长度. 一.优点 1. 支持自动改变大小的功能 ...

  7. Table ‘performance_schema.session_variables’ doesn’t exist

    运行mysql时,提示Table ‘performance_schema.session_variables’ doesn’t exist 解决的方法是: 第一步:在管理员命令中输入: mysql_u ...

  8. MySQL使用rand函数实现随机数[转]

    如何写一个语句能一下更新几百条MYSQL数据! 需要测试MYSQL数据库,里面有一个上万条数据的数据库,如何写一个PHP文件一下每次更新几百条信息,我都是写一个循环一次更新一条信息,这样我知道用WHI ...

  9. mysql开启日志

    在 centos 5 下,  在 mysld 下面,添加一行 log=/var/log/mysql.log 然后执行如下命令 touch /var/log/mysql.logchmod 777 /va ...

  10. 66.为什么有时候在ISE软件中,顶层文件不能置顶?

    什么时候回出现顶层文件不能置顶呢?嘿嘿,肯定是工程中有错误啦. 如果你的顶层文件包含了include文件,这个时候就会出现这种情况了.但好像出现在刚新建工程的时候,因为当顶层文件不包括Include文 ...