<分区>
这是我在一次面试中遇到的算法题之一。无法弄清楚如何以最有效的方式解决它。
<分区>
这是我在一次面试中遇到的算法题之一。无法弄清楚如何以最有效的方式解决它。
最佳答案
这是我建议的代码。它在 long
范围内找到 0 和 7 之间的最小数字(数字 0 除外)。
在这种情况下,我正在寻找 11 的结果。
public class Class007
{
static long NUM = 11;
public static void main(String[] args)
{
//NUM is the given number
//find007() finds the smallest number with 0 & 7 that is divided by NUM
System.out.print(find007(NUM));
}
static long find007(long n){
if(is007(n))
return n;
if(n+NUM<n)
return 0;
return find007(n+NUM);
}
static boolean is007(long n){
while(n!=0 && (n%10==0 || n%10==7))
n=n/10;
return n==0;
}
}
关于java - 如何找到只有 0 和 7 除以给定数字的最小数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29298656/
相关文章:
javascript - 有什么办法可以避免 for 循环? (ES6/JavaScript)
algorithm - 在哪里可以找到 Cormen 装配线调度的在线示例?
java - 客户端-服务器套接字 : Not reaching if statement
java - ThreadPool 中的 Runnable 与线程内和 ThreadPool 中的 Runnable 之间有什么区别
java - EJB。它是什么,为什么存在以及它是如何工作的?
java - 在 Java 中提取媒体属性或将 MediaInfo 安装到 Eclipse 中