int中的所有数字都可以被某些int整除

时间:2015-11-08 22:33:44

标签: java

我试图弄清楚如何计算两个整数(a和b)之间的所有数字,其中所有数字都可以用另一个int(k)整除,0计算为可分数。这就是我所知道的到目前为止,但它永远是循环的。

for (int i = a; i<=b; i++){
   while (i < 10) {
      digit = i % 10;
      if(digit % k == 0  || digit == 0){
         count ++;
      }
      i = i / 10;
   }
}

此外,我正在考虑比较所有数字是否可以通过计算它们并与数字位数int length = (int)Math.Log10(Math.Abs(number)) + 1;进行比较

来进行比较

任何帮助将不胜感激。谢谢!

3 个答案:

答案 0 :(得分:3)

一旦你进入while区块,你永远不会离开它。 while条件是i小于10时的情况。您在i块的末尾将whole除以10。 i永远不会有机会超过10。

答案 1 :(得分:0)

试试这个

public class Calculator {

    public static void main(String[] args) {
        int a = 2;
        int b = 150;
        int k = 3;
        int count = 0;
        for (int i = a; i <= b; i++) {
            boolean isDivisible = true;
            int num = i;
            while (num != 0) {
                int digit = num % 10;
                if (digit % k != 0) {
                    isDivisible = false;
                    break;
                }
                num /= 10;
            }
            if (isDivisible) {
                count++;
                System.out.println(i+" is one such number.");
            }
        }
        System.out.println("Total " + count + " numbers are divisible by " + k);
    }
}

答案 2 :(得分:0)

好的,所以这里有很多事情要发生,所以我们一次只能拿这件事。

for (int i = a; i <= b; i++){

   // This line is part of the biggest problem. This will cause the
   // loop to skip entirely when you start with a >= 10. I'm assuming
   // this is not the case, as you are seeing an infinite loop - which
   // will happen when a < 10, for reasons I'll show below.
   while (i < 10) {
      digit = i % 10;
      if(digit % k == 0  || digit == 0){
         count ++;

         // A missing line here will cause you to get incorrect 
         // results. You don't terminate the loop, so what you are 
         // actually counting is every digit that is divisible by k 
         // in every number between a and b.
      }

      // This is the other part of the biggest problem. This line 
      // causes the infinite loop because you are modifying the 
      // variable you are using as the loop counter. Mutable state is
      // tricky like that.
      i = i / 10;
   }
}

可以用最少的更改重新编写它,但是您可以进行一些改进,以提供更可读的结果。这段代码是未经测试的,但是会编译,并且应该可以帮助你完成大部分工作。

// Extracting this out into a function is often a good idea.
private int countOfNumbersWithAllDigitsDivisibleByN(final int modBy, final int start, final int end) {
    int count = 0;

    // I prefer += to ++, as each statement should do only one thing,
    // it's easier to reason about
    for (int i = start; i <= end; i += 1) {

        // Pulling this into a separate function prevents leaking
        // state, which was the bulk of the issue in the original.
        // Ternary if adds 1 or 0, depending on the result of the
        // method call. When the methods are named sensibly, I find
        // this can be more readable than a regular if construct.
        count += ifAllDigitsDivisibleByN(modBy, i) ? 1 : 0;
    } 
    return count;
}

private boolean ifAllDigitsDivisibleByN(final int modBy, final int i) {
    // For smaller numbers, this won't make much of a difference, but
    // in principle, there's no real reason to check every instance of 
    // a particular digit.
    for(Integer digit : uniqueDigitsInN(i)) {
        if ( !isDigitDivisibleBy(modBy, digit) ) {
            return false;
        }
    }
    return true;
}

// The switch to Integer is to avoid Java's auto-boxing, which 
// can get expensive inside of a tight loop.
private boolean isDigitDivisibleBy(final Integer modBy, final Integer digit) {
    // Always include parens to group sub-expressions, forgetting the 
    // precedence rules between && and || is a good way to introduce 
    // bugs.
    return digit == 0 || (digit % modBy == 0);
}

private Set<Integer> uniqueDigitsInN(final int number) {
   // Sets are an easy and efficient way to cull duplicates.
   Set<Integer> digitsInN = new HashSet<>();
   for (int n = number; n != 0; n /= 10) {
       digitsInN.add(n % 10);
   }
   return digitsInN;
}