如何打印递归跟踪(阶乘函数)?

时间:2014-02-13 08:00:50

标签: javascript recursion

为了更好地理解递归,我试图弄清楚如何将递归跟踪记录到控制台。我有'追踪'部分,但我不确定如何“冒泡”解决方案。有关完美放置的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编写):

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  

非常酷,对吗?经过更多的实验,我仍然无法做到这一点......

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

我认为最好通过使用您的示例(编辑一点)和评论来解释。假设您在第一次将参数设置为 5 时调用此函数。

// 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)

答案 2 :(得分: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;
        }
    }