如何找到的最小数与仅有0和7是由一个给定的数量除以? [英] How to find the smallest number with just 0 and 7 which is divided by a given number?

查看:148
本文介绍了如何找到的最小数与仅有0和7是由一个给定的数量除以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是算法的问题,我在采访的一个遇到之一。无法弄清楚如何解决它最有效的方式。

This is one of algorithmic problem I encountered in one of interview. Unable to figure out how to solve it in most efficient way.

推荐答案

下面是我的建议code。它发现的最小数为0和7(除0号)的范围内。 在这种情况下,我正在寻找的结果为11。

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;
     }
}

这篇关于如何找到的最小数与仅有0和7是由一个给定的数量除以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆