This is one of algorithmic problem I encountered in one of interview. Unable to figure out how to solve it in most efficient way.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has answers here:
Closed 5 years ago.
回答1:
Here is my suggested code. It finds the smallest number with 0 and 7 (except the number 0) within the long
range.
In this case, I'm looking for the result for 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;
}
}
回答2:
According by THIS QUESTION, in http://oeis.org/ you can find this class of number sequences: Check your's here.
a(n) = min{A204094(k): k > 0 and A204094(k) mod n = 0}
Simply adapt the algorithm to fit your needs.