PHP-递归

1.3 递归

函数内部自己调用自己

递归有两个元素,一个是递归点(从什么地方递归),第二递归出口

例题1:输出9 8 7 6 …

<?php
function printer($num) {
	echo $num,'&nbsp;';
	if($num==1)	//递归出口
		return;
	printer($num-1);	//递归点
}
printer(9);	//9 8 7 6 5 4 3 2 1 

例题2:从1加到100

function cal($num) {
	if($num==1)
		return 1;
	return $num+cal($num-1);
}
echo cal(100);
//分析
/**
第$i次执行			结果
cal(100)			100+cal(99)
=					100+99+cal(98)
=					100+99+98+cal(97)
=					100+99+98+++++cal(1)
=					100+99+98++++1
*/

例题:打印前10个斐波那契数列

//打印第5个斐波那契数
function fbnq($n) {
	if($n==1 || $n==2)
		return 1;
	return fbnq($n-1)+fbnq($n-2); //第n个斐波那契数等于前两个数之和
}
echo fbnq(5),'<br>';
/**
*分析:
fbnq(5)	=fbnq(4)+fbnq(3)
		=fbnq(3)*2+fbnq(2)
		=(fbnq(2)+fbnq(1))*2+fbnq(2)
		=(1+1)*2+1
		=5
*/
//打印前10个斐波那契数
for($i=1;$i<=10;$i++)
	echo fbnq($i),'&nbsp;';   //1 1 2 3 5 8 13 21 34 55 

小结:递归尽量少用,因为递归需要用到现场保护,现场保护是需要消耗资源的

发布了1748 篇原创文章 · 获赞 1839 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/weixin_42528266/article/details/105094004