【问题标题】:How would you solve this problem using recursion and without other looping methods?您将如何使用递归而不使用其他循环方法来解决这个问题?
【发布时间】:2020-08-24 05:51:54
【问题描述】:

下面的函数返回 num1 / num2 之间 2 的倍数。 因此,如果 num 1 = 8, num 2 = 12,它将返回 3,因为它是等于 8、10、12 的倍数。 您将如何仅使用递归来复制此函数?

function makeMultiplesOfDigit(num1, num2) {
  let count = 0;
  let start = num1;
  let end = num2;

  if (num1 > num2) {
    start = num2;
    end = num1;
  }

  if (start === 0) {
    start = 1;
  }

  for (let i = start; i <= end; i++) {
    if (i % 2 === 0) {
      count += 1;
    }
  }

  return count;
}

【问题讨论】:

  • 为什么是if (start === 0)?书上说0是偶数,所以应该算一。更重要的是,如果num1 是负数,num2 是正数,那么0 算作偶数之一。所以这是没有意义的。
  • 抱歉回复晚了。这是一个最初为刚开始学习编码的初学者编写的问题。我猜他们(训练营)试图通过给 0 例外来使其更容易。现在从我第一次看到这个问题已经过去了大约 3 周,我正在尝试应用我从递归中学到的东西,以便我可以练习。顺便说一句,我非常感谢您的清晰解释。非常感谢。
  • 请注意,问题中的函数名称与它应该做的完全不匹配。也许像countEvens

标签: javascript recursion


【解决方案1】:

递归并不是解决这个问题的正确方法,因为它有一个封闭的公式,但是你去吧:

function makeMultiplesOfDigit(num1, num2) {
  if (num1 > num2) return makeMultiplesOfDigit(num2, num1);
  let isEven = 1 - num1 % 2;
  if (num1 == num2) return isEven; // base case
  return isEven + makeMultiplesOfDigit(num1 + 1, num2);
}

// demo
console.log(makeMultiplesOfDigit(8, 12));

注意:当num1 为 0 时,您的代码不会将 0 视为偶数,这很奇怪:0 肯定是偶数。如果函数调用时使用 (-1, 1),那么它仍然会将 0 视为偶数,因此也不一致。

作为附加信息,这里是封闭公式版本:

function makeMultiplesOfDigit(num1, num2) {
  return ((Math.abs(num1 - num2) + 1) >> 1) + (num1 % 2 === 0 && num2 % 2 === 0);
}

// demo
console.log(makeMultiplesOfDigit(8, 12));

【讨论】:

    【解决方案2】:
    function makeMultiplesOfDigit(num1,num2,count=0){
            if (num1 % 2 === 0 ){
                count++
             }
            if(num1>=num2){
                return count
             }
            else{
               count = makeMultiplesOfDigit(num1+1,num2,count)
               return count
             }
          }
      
    
        makeMultiplesOfDigit(8, 12)
    
    
        
    

    【讨论】:

      【解决方案3】:

      看到了类似的回复,反正给你。

      function getMultiplesInRange(num1, num2, num3 = 0) {
        if (num1 > num2) return num3
        const val = num1 % 2 === 0 ? 1 : 0
        return getMultiplesInRange(num1 + 1, num2, val + num3)
      }
      
      console.log(getMultiplesInRange(8, 12))

      【讨论】:

        【解决方案4】:

        如果您只处理第一个数字小于第二个数字的情况:

        // Generic utils
        const isEven = (n) => n % 2 === 0 
        const eq = (a, b) => a === b
        const inc = (n) => n + 1
        const add = (a, b) => a + b
        
        // Constants
        const incCount = 1
        const leaveCount = 0
        
        // Implementation
        const shouldIncCount = (n) => isEven(n) ? incCount : leaveCount
        const makeMultiplesOfDigit = (a, b) => eq(a, b) ? shouldIncCount(a) : add(shouldIncCount(a), makeMultiplesOfDigit(inc(a), b));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-12-02
          • 1970-01-01
          • 1970-01-01
          • 2021-06-14
          • 1970-01-01
          • 2018-01-11
          • 2021-01-11
          相关资源
          最近更新 更多