Skip to content

递归

注意不要思考深层递归,只分析当前层,或一两层

1 分析运行结果

观察以下最简单的指针代码,它的运行结果会是怎么样呢?

cpp
#include <iostream>
using namespace std;

void hello(){
    cout << "hello" << endl;
    hello();
    cout << "world" << endl;
}

int main() {
    hello();
    return 0;
}
cpp
#include <iostream>
using namespace std;

void hello(int a){

    if(a==3) return;

    cout << "hello" << endl;
    a++;
    hello(a);

}

int main() {
    hello(1);
    return 0;
}
cpp
#include <iostream>
using namespace std;

void hello(int a){

    if(a==3) return;

    cout << "递阶段" << endl;
    hello(++a);
    cout << "归阶段" << endl;
}

int main() {
    hello(1);
    return 0;
}
cpp
#include <iostream>
using namespace std;

void hello(int a){

    if(a==4) return;

    cout << a << endl;
    a = a + 2;
    hello(a);
}

int main() {
    hello(0);
    return 0;
}
输出示例

输入密码才能查看参考答案噢