简体   繁体   English

需要帮助了解递归

[英]Need help understanding recursion

So in my class we are studying recursive functions. 因此,在我的class我们正在研究递归函数。 But I just do not understand how they work. 但是我只是不明白它们是如何工作的。

I'll use this code as an example. 我将使用此代码作为示例。

// n is written to the screen vertically
// with each digit on a separate line.

void write_vertical(int n) {

    if (n < 10) 
    {
        cout << n << endl;
    }
    else   // n is two or more digits long
    { 
        write_vertical(n/10);
        cout << (n % 10) << endl;
    }
}

So if int n = 123; 所以如果int n = 123; it will print each digit on its own line.How does this happen? 它会在自己的行上打印每个数字。这是怎么发生的? how does this function work step by step? 此功能如何逐步工作?

Testing by random number (13) 通过随机数进行测试(13)

Let's take 13 for an example. 让我们以13为例。 Well it's not less than 10, so it will execute the else block, and it will immediately execute the function itself with 13/10, which (as an integer) is 1. Now 1 is less than 10, so it will print 1 onto the screen with a new line because of endl . 好吧,它不少于10,因此它将执行else块,并且它将立即以13/10(作为整数)将1本身执行函数。现在1小于10,因此将1打印到由于endl ,屏幕换行了。 Now it will return back to the previous function call it had (before calling the function again with argument 1) and execute 13%10 . 现在,它将返回到之前调用的函数(在再次使用参数1调用该函数之前)并执行13%10 13 modulus 10 is 3, since its remainder becomes 3, with a new line (again because of endl ). 13模数10为3,因为它的余数变为3,并且换行了(同样由于endl )。 And voila, you printed the number in a vertical line! 瞧,您将数字垂直打印了!

For future 为了未来

You should use a pencil and a paper, and debug it yourself manually just like we did above. 您应该使用铅笔和纸,然后像上面一样手动进行调试。 OR even better use a debugger like GDB ! 甚至更好的使用像一个调试器GDB This is an excellent quick startup on how to debug with GDB. 是关于如何使用GDB进行调试的极好的快速入门。

1: 1:

if(123 < 10)     // fails
    cout << 123; // skipped
else
{
    recurse(123 / 10); // recurse(12) converted to int
    cout << 123 % 10; // i'll be back here wait
}

2: 2:

if(12 < 10)     // fails
    cout << 12; // skipped
else
{
    recurse(12 / 10); // recurse(1)
    cout << 12 % 10; // wiat I'll be back
}

3: 3:

if(1 < 10)      // succeeds! so now else executed
    cout << 1; // printed 

nothing is below until return of functions so we return to 2: 在返回函数之前,下面什么都没有,所以我们回到2:

cout << 12% 10; // (12 % 10 = 2) was wating it's its time

continue below: nothing below so return from function 2 to 1: 在下面继续:在下面什么都没有,所以从功能2返回到1:

in 1: 在1中

cout << 123 % 10; // it was waiting so now it's its time
cout << 123 % 10; // 123 % 10 = 3

go below: nothing untile the end of function so retrun to main where function was called first time (to the line after the call ) 转到下面:在函数结束之前什么都没有,所以重新运行到第一次调用该函数的main(到调用之后的行)

the result: 123 结果:123

Recursion is simple. 递归很简单。 Assume you've already written your function; 假设您已经编写了函数; then use it. 然后使用它。

Here it's the other way around - you are trying to figure out what the function does. 这是另一种方式-您试图弄清楚函数的作用。 Just the same, when you call it, it always does the same stuff. 一样,当您调用它时,它总是做同样的事情。

So, in a general case of a number n > 10 , what is n/10 (integer division)? 因此,在n > 10的一般情况下, n/10 (整数除法)是多少? It's that number without its last decimal digit. 那是没有最后一个十进制数字的数字。

What's n % 10 ? n % 10多少? It's the number's last decimal digit. 这是数字的最后一个十进制数字。

So, your definition reads: 因此,您的定义为:

doing_something for a number `n` IS

    doing_it for this number without its last decimal digit 
                         (if there's something left, that is);

    then printing its last decimal digit and a newline after it.

That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM