2014-02-13 50 views
1

爲了更好地理解遞歸,我試圖弄清楚如何將遞歸跟蹤記錄到控制檯。我有'追蹤'部分,但我不確定如何「解決」這個問題。對完美放置console.log語句的任何建議?如何打印遞歸軌跡(階乘函數)?

這裏是我到目前爲止有:

function factorial (num) { 
    if (num === 1) { 
     console.log('factorial(' + num + ') = ' + num); 
     return 1; 
    } else { 
     console.log('factorial(' + num + ') = ' + num + ' * ' + 'factorial(' + (num - 1) + ')'); 
     return num * factorial(num - 1); 
    } 
} 

它打印以下控制檯:

factorial(5) = 5 * factorial(4) 
factorial(4) = 4 * factorial(3) 
factorial(3) = 3 * factorial(2) 
factorial(2) = 2 * factorial(1) 
factorial(1) = 1 
120 

但大約1 * 2 * 3 * 4 * 5的部分是什麼?我知道它發生在那裏,我怎麼打印它?

我猜我希望它看起來像這樣:

1 
1 * 2 
2 * 3 
6 * 4 
24 * 5 
120 

感謝您的任何建議!

經過搜索,我發現這個過在CodeRanch,不幸的是SANS代碼(用Java編寫的)OK:

Enter fact(6) 
    Enter fact(5) 
     Enter fact(4) 
      Enter fact(3) 
       Enter fact(2) 
        Enter fact(1) 
         Enter fact(0) 
         0!Ret: 1 
        Ret: 1 * fact(n-1) = 1 * fact(0) = 1 * 1 = 1 
       Ret: 2 * fact(n-1) = 2 * fact(1) = 2 * 1 = 2 
      Ret: 3 * fact(n-1) = 3 * fact(2) = 3 * 2 = 6 
     Ret: 4 * fact(n-1) = 4 * fact(3) = 4 * 6 = 24 
    Ret: 5 * fact(n-1) = 5 * fact(4) = 5 * 24 = 120 
Ret: 6 * fact(n-1) = 6 * fact(5) = 6 * 120 = 720 
fact(6) = 720 

很酷,不是嗎?經過更多的嘗試,我仍然無法實現這個雖然...

+0

你能後你期望的輸出的例子嗎?你也可以簡單地記錄一次語句的階乘。 – elclanrs

+1

剛剛添加我的預期輸出:) –

+0

找到答案,我已經提到。 它應該工作。 –

回答

0

我認爲這是最好的解釋通過使用你的例子(編輯一點)的意見。假定您第一次使用參數設置爲來調用此函數。

// num = 5, the first time it's called 
function factorial (num) { 
    console.log('factorial(' + num + ')'); 

    if (num === 1) { 
     // If num === 1, the function will just return 1 and exit. 
     return 1; 
    } else { 
     // Otherwise, which happens almost every time (since 1 is only 
     // reached once and then it stops). For 5, this would return 
     // 5 * factorial(4), which in order returns 4 * factorial(3), 
     // and so on. 
     return num * factorial(num - 1); 
    } 
} 

此輸出可以幫助您瞭解:

factorial(5) == (5) * (factorial(4)) // (5) * (4 * 3 * 2 * 1) 
factorial(4) == (4) * (factorial(3)) // (4) * (3 * 2 * 1) 
factorial(3) == (3) * (factorial(2)) // (3) * (2 * 1) 
factorial(2) == (2) * (factorial(1)) // (2) * (1) 
factorial(1) == (1)     // (1) 
+0

也許我還是感到困惑...... //右邊的東西 - >這一切都發生在函數內部,對吧?那麼難道沒有辦法按照您輸入的內容完全登錄嗎? –

+0

@i_made_that不,不完全是,因爲在第一行,我只知道'5'和'4 * 3 * 2 * 1'部分是以階乘(4)計算的。我會稍微編輯一下,以便更清楚。 – Broxzier

+0

是的,這是我撓我的頭:)當我們到達最後一行(factorial(1)== 1)時,factorial必須通過嵌套函數傳遞計算值BACK不是嗎?在我想象中的世界中,我可以用一個巧妙定位的console.log來捕獲它... –

2
function factorial (num) { 
    if (num === 1) { 
     console.log(num); //print new line after this 
     return 1; 
    } else { 
     var val = factorial(num - 1); 
     console.log(num +'*' + val); //print new line after this 
     return num * val; 
    } 
} 
+0

這絕對是越來越近了!但我想捕捉整個痕跡,而不僅僅是最後一個...... –

0
function factorial (num) { 
     for (var i=1; i<6-num; i++) 
      console.log(' '); 
     } 
     console.log('Enter fact('+num+')'); //add code to move new line 
     if(num==0) { 
      for (var i=1; i<6-num; i++) 
       console.log(' '); 
      } 
      console.log('0!Ret: 1 '); // add code to move new line 
      return 1; 
     } else { 
      int val = factorial(num-1); 
      for (var i=1; i<6-num; i++) 
       console.log(' '); 
      } 
      console.log('Ret:'+num+ ' * fact(n-1) = ' + num+ ' * fact('+(num-1)+') = '+num+' * ' + val + ' = ' + (num*val)); // add code to move new line 
      return num*val; 
     } 
    } 
+0

將兩個答案合併成一個會更好。 – Makoto

+0

我該怎麼做? –