About recursive analysis in php

不言
Release: 2023-03-31 13:58:02
Original
1701 people have browsed it

This article mainly introduces the recursive analysis in php, which has certain reference value. Now I share it with you. Friends in need can refer to it

What is recursion? ?

Give a popular example:

#There is an 8-pound apple that you want to cut into pieces of equal weight. Several portions, each weighing no more than 1 tael. You will definitely think of doing this:

1.First cut an apple into 2 parts of equal weight, A1 and A2. ;

2.Cut one portion of A1 into two equal parts, A11 and A12, and cut A2 into equal parts. Two parts A21 and A22;

3.Cut A11 into two equal parts...

4.Until each small portion is less than or equal to 1 liang.

#The above example is a recursive model, turning a large thing into several small things, and the method is the same every time.

ProgramSelfThe programming technique of calling itself is called Recursion(recursion. Recursion includes direct recursion and indirect recursion

•Direct recursion: The function calls itself during execution.

•Indirect recursion: A function calls other functions during execution and then calls itself through these functions.

Recursion has four characteristics:

1. There must be a termination condition that can be finally reached, otherwise the program will fall into an infinite loop;

2.The sub-problem is larger in scale than the original problem Small, or closer to the termination condition;

3.The sub-problem can be solved by recursively calling again or directly because the termination condition is satisfied ;

4.The solutions to the sub-problems should be able to be combined into the solution to the entire problem.

The above example also satisfies the above four properties:

(1).The termination condition isThe weight of each portion cannot be greater than 1;(2).The size of each cut must be larger than The last time was small;(3).The method of cutting is the same every time, so the sub-problem can be called recursively;(4).Every time it is finally cut A small portion is the solution required.

function cutApple($w,$t){
	if($w<1){
		echo "已经切完了";
	}
	else{
		echo "第".$t."次切割,每份重量是:".($w/2)."<br>";
		$t+=1;
		cutApple($w/2,$t);
	}
}
Copy after login

Result:

##1st cutting, The weight of each portion is: 7.5

The second cutting, the weight of each portion is: 3.75
The 3rd cut, the weight of each piece is: 1.875
The 4th cut, the weight of each piece is: 0.9375

##Has been cut

Another popular example: try to find the sum of the arithmetic sequence: 1 2 3 4 ... 98 100;

/**
 * 等差数列求和
 * @param [int] $n [第一个数]
 * @param [int] $m [第二个数]
 */
function add($n,$m){
	if(!is_int($n)||!is_int($m)) 
		return false;
	static $sum=0;
	$sum+=$n;
	$n++;
	if($n>$m) return $sum;
	return add($n,$m);
}
echo add(1,100);
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

PHP如何动态修改配置文件

The above is the detailed content of About recursive analysis in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template