文章目录
一、什么是递归
函数调用自己的情况叫做递归。
注意:递归必须有退出条件,不可以无限调用自己,否则会导致程序奔溃
二、应用
1、 计算1+2+3
#include<iostream>
using namespace std;
int num = 0;
int calculate(int x)
{
num++;
if (x > 0)
{
return x + calculate(x - 1);
}
else
{
return 0;
}
}
int main()
{
int result = calculate(3);
cout << "result =" << result << endl;
cout << "calculate函数共运行" << num << "次" << endl;
system("pause");
return 0;
}
result =6
calculate函数共运行4次
计算过程如下所示
计算的就是3+2+1+0。
分析:
共4层递归,调用了calculate函数4次,返回4-1=3次。
每一层递归都会在栈区开辟新内存用于保留变量等数据,程序共占用4块相同大小的内存空间。
2、计算阶乘
#include<iostream>
using namespace std;
int num = 0;
int calculate(int x)
{
num++;
if (x > 0)
{
return x * calculate(x - 1);
}
else
{
return 1;
}
}
int main()
{
int result = calculate(3);
cout << "result = " << result << endl;
cout << "calculate函数共运行" << num << "次" << endl;
system("pause");
return 0;
}
result = 6
calculate函数共运行4次
计算的就是321*1。
分析:
共4层递归,调用了calculate函数4次,返回4-1=3次。
每一层递归都会在栈区开辟新内存用于保留变量等数据,程序共占用4块相同大小的内存空间。
3、辗转相除法求最大公约数
#include<iostream>
using namespace std;
int num = 0;
int gcd(int a, int b)
{
num++;
if (a % b == 0)
{
return b;
}
else
{
return gcd(b, a % b);
}
}
int main()
{
int result = gcd(32, 100);
cout << "result = " << result << endl;
cout << "gcd函数共运行" << num << "次" << endl;
system("pause");
return 0;
}
result = 4
gcd函数共运行3次
最终将4一层层返回,结果就是4。
分析:
共3层递归,调用了gcd函数3次,返回3-1=1次。
每一层递归都会在栈区开辟新内存用于保留变量等数据,程序共占用3块相同大小的内存空间。
4、求菲波那契数列
斐波那契数列指的是这样一个数列:1,1,2,3,5,8,13,21,34,55,89…
这个数列从第3项开始,每一项都等于前两项之和。
用程序计算第n个数是多少:
#include<iostream>
using namespace std;
int Fibonacci(int n)
{
if (n == 1)
{
return 1;
}
else if (n == 2)
{
return 1;
}
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
int main()
{
int result = Fibonacci(5);
cout << "result = " << result << endl;
system("pause");
return 0;
}
result = 5
Fibonacci函数共运行9次
这之前的例子都是单层递归,每次只等待一个函数的结果;
求菲波那契数列属于双层递归,每次要等待两个函数的结果。
分析:
共3大层递归,调用了Fibonacci函数9次。
每一层递归都会在栈区开辟新内存用于保留变量等数据,程序共占用9块相同大小的内存空间。
下图显示的是Fibonacci调用次数与n的关系,可以看到两者几乎成指数级关系。
随着n的变大,程序占用内存会呈指数级增长,直至栈溢出。
5、判断数组是否为升序排列
#include<iostream>
using namespace std;
bool fun(int* p, int n)
{
if (1 == n)
{
return true;
}
else if (2 == n)
{
return p[n - 1] >= p[n - 2];
}
else
{
return (fun(p, n - 1) && p[n - 1] >= p[n - 2]);
}
}
int main()
{
int p[] = { 1,2,3,4,6,5 };
int a = fun(p, sizeof(p) / sizeof(int));
if (true == a)
{
cout << "数组为升序排列" << endl;
}
else
{
cout << "数组不是升序排列" << endl;
}
system("pause");
return 0;
}
三、递归算法的缺陷
递归算法代码简洁,易于理解,但凡事皆有代价!
1、巨大的空间消耗
程序的局部变量、局部数组、形参、寄存器、冗余数据等都会压入栈中保存,栈空间是有限的,VS2019的默认栈空间为1M。如下图所示:
一般函数调用完,占用的栈空间就会即可被释放;
但递归函数不会,每一层的递归调用都会在栈上分配一块内存,有多少层递归调用就分配多少块相似的内存,所有内存加起来的总和是相当恐怖的,很容易导致栈溢出。
2、巨大的时间开销
每次调用函数都会在栈上分配内存,函数调用结束后再释放这一部分内存,内存的分配和释放都是需要时间的。
每次调用函数还会多次修改寄存器的值,函数调用结束后还需要找到上层函数的位置再继续执行,这也是需要时间的。
所有的这些时间加在一起是非常恐怖的。
#include<iostream>
using namespace std;
int num = 0;
int num_return = 0;
int Fibonacci(int n)
{
num++;
if (n == 1)
{
num_return++;
return 1;
}
else if (n == 2)
{
num_return++;
return 1;
}
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
int main()
{
clock_t time_start, time_end;
time_start = clock();
int result = Fibonacci(40);
time_end = clock();
cout << "result = " << result << " ";
cout << "共运行" << num << "次" << endl;
cout << "运行时间" << (double)(time_end - time_start) / CLOCKS_PER_SEC << "s" << endl;
system("pause");
return 0;
}
result = 102334155 共运行204668309次
运行时间4.084s
作为对比,用迭代法实现斐波那契数列
#include<iostream>
using namespace std;
int num = 0;
int num_return = 0;
int Fibonacci_Iteration(int n)
{
int result = 1;
int pre;
int next = 1;
while (n > 2)
{
n--;
pre = next;
next = result;
result = next + pre;
}
return result;
}
int main()
{
clock_t time_start, time_end;
time_start = clock();
int result = Fibonacci_Iteration(40);
time_end = clock();
cout << "result = " << result << endl;
cout << "运行时间" << (double)(time_end - time_start) / CLOCKS_PER_SEC << "s" << endl;
system("pause");
return 0;
}
result = 102334155
运行时间0s
原理如下所示:
时间快到clock() 函数已经分辨不出来了。
基于以上两个严重缺陷,一般能不用递归就不用递归。
参考资料
2、https://blog.csdn.net/qq_43205256/article/details/123069982