文章目录

1.练习代码

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
using namespace std;
void up_and_down(int);

int _tmain(int argc, _TCHAR* argv[])
{
	up_and_down(1);
	return 0;
}

void up_and_down(int n)
{
	printf("Level %d: n location %p\n", n, &n);
	if (n < 4)
		up_and_down(n+1);
	printf("Level %d: n location %p\n", n, &n);
}

2.关键点分析

2.1递归过程

第一级递归:打印Level1,1<4,进入第二级递归
第二级递归:打印Level2,2<4,进入第三级递归
第三级递归:打印Level3,3<4,进入第四级递归
第四级递归:打印Level4,4<4为假,再打印一次Level4后,函数执行完毕,控制权返回第三级递归
第三级递归继续执行:打印Level3,函数执行完毕,控制权返回第二级递归
第二级递归继续执行:打印Level2,函数执行完毕,控制权返回第一级递归
第一级递归继续执行:打印Level1,函数执行完毕,控制权返回main主函数

2.2运行结果

【小练习】循环、递归与概率:递归-LMLPHP

10-04 14:45