A positive integer is called a “self-divisor” if every decimal digit of the number is a divisor of the number, that is, the number is evenly divisible by each and every one of its digits. For example, the number 128 is a self-divisor because it is evenly divisible by 1, 2, and 8. However, 26 is not a self-divisor because it is not evenly divisible by the digit 6. Note that 0 is not considered to be a divisor of any number, so any number containing a 0 digit is NOT a self-divisor. There are infinitely many self-divisors.

Write method firstNumSelfDivisors, which takes two positive integers as parameters, representing a start value and a number of values. Method firstNumSelfDivisors returns an array of size num that contains the first num self-divisors that are greater than or equal to start. For example, the call firstNumSelfDivisors(10, 3) should return an array containing the values 11, 12, and 15, because the first three self-divisors that are greater than or equal to 10 are 11, 12, and 15. Be sure to use the method isSelfDivisor in your answer which we wrote in an earlier section.

public class SelfDivisor
{

   /** @param number the number to be tested
    *         Precondition: number > 0
    *  @return true if every decimal digit of
    *          number is a divisor of number;
    *          false otherwise
    */
   public static boolean isSelfDivisor(int number)
   {
     int currNumber = number;
     int digit = 0;
     while (currNumber > 0)
     {
        digit = currNumber % 10;
        if (digit == 0) return false;
        if (number % digit != 0) return false;
        currNumber = currNumber / 10;
     }
     return true;
   }

   /**
    * @param start starting point for values to be checked
    * Precondition: start > 0
    * @param num the size of the array to be returned
    * Precondition: num > 0
    * @return an array containing the first num
    * integers >= start that are self-divisors
    */
   public static int[] firstNumSelfDivisors(int start,
                                            int num)
   { /* to be implemented in part (b) */ }

   public static void main (String[] args)
   {
     System.out.println("Self divisors for firstNumSelfDivisors(10, 3):");
     for (int n : firstNumSelfDivisors(10, 3))
        System.out.print(n + " ");
     System.out.println();

     System.out.println("Self divisors for firstNumSelfDivisors(22, 5)");
     for (int n : firstNumSelfDivisors(22, 5))
        System.out.print(n + " ");
     System.out.println();
   }
 }

如何解题

首先我们试着手算一下。问题让我们返回一个大小为num的数组,因而我们需要先创建这样的数组。在没找到给定数量的自我除数之前,循环要一直进行并尝试当前值。我们还需要记录我们所找到的所有自我除数。对于firstNumSelfDivisors(10, 3)而言,我们需要尝试10(false)、11(true)、12(true)、13(false)、14(false)和15(true)。



0:00

Which loop should you use to loop through all the digits of the number?

当不明确循环次数的时候,使用while
3

Which of the following correctly declares and creates the array to return?

需要先声明数组,再用new来创建。
4

以下是本题的参考答案。

public static int[] firstNumSelfDivisors(int start, int num) {
  int[] selfs = new int[num];
  int numStored = 0;
  int nextNumber = start;
  while (numStored < num) {
    if (isSelfDivisor(nextNumber)) {
      selfs[numStored] = nextNumber;
      numStored++;
    }
    nextNumber++;
  }
  return selfs;
}

陈 欣

AADPS创始人

发表评论