Home > php教程 > php手册 > 讲述PHP递归算法

讲述PHP递归算法

WBOY
Release: 2016-06-13 11:04:49
Original
1262 people have browsed it

PHP还是比较常用的,于是我研究了一下PHP递归算法,在这里拿出来和大家分享一下,希望对大家有用。PHP,一个嵌套的缩写名称,是英文超级文本预处理语言(PHP:Hypertext Preprocessor)的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,现在被很多的网站编程人员广泛的运用。PHP 独特的语法混合了 C、Java、Perl 以及 PHP 自创新的语法。

它可以比 CGI 或者 Perl 更快速的执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML文档中去执行,执行效率比完全生成HTML标记的CGI要高许多;与同样是嵌入HTML文档的脚本语言JavaScript相比,PHP在服务器端执行,充分利用了服务器的性能;PHP执行引擎还会将用户经常访问的PHP程序驻留在内存中,其他用户再一次访问这个程序时就不需要重新编译程序了,只要直接执行内存中的代码就可以了,这也是PHP高效率的体现之一。

PHP具有非常强大的功能,所有的CGI或者JavaScript的功能PHP都能实现,而且支持几乎所有流行的数据库以及操作系统。我们这里详细的介绍一下PHP递归算法。

PHP递归算法代码:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li class=""><span>//定义PI一分的角度的值  </span></li>
<li class="alt"><span>define("PII",M_PI/180);  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//新建图像资源,并定义其背景为白色,前景色为黑色  </span></li>
<li class="">
<span>$</span><span class="attribute">im</span><span>=</span><span class="attribute-value">imagecreate</span><span>(670,500);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">white</span><span>=</span><span class="attribute-value">imagecolorallocate</span><span>($im,0xFF,0xFF,0xFF);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">g</span><span>=</span><span class="attribute-value">imagecolorallocate</span><span>($im,0x00,0x00,0x00);  </span>
</li>
<li class="alt"><span> </span></li>
<li class=""><span>//从下面实例化的代码可以得知,初始值$x,$y,$L,$a别分为300,500,100,270  </span></li>
<li class="alt"><span>functiondrawLeaf($g,$x,$y,$L,$a){  </span></li>
<li class=""><span>global$im;  </span></li>
<li class="alt">
<span>$</span><span class="attribute">B</span><span>=</span><span class="attribute-value">50</span><span>;  </span>
</li>
<li class="">
<span>$</span><span class="attribute">C</span><span>=</span><span class="attribute-value">9</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">s1</span><span>=</span><span class="attribute-value">2</span><span>;  </span>
</li>
<li class="">
<span>$</span><span class="attribute">s2</span><span>=</span><span class="attribute-value">3</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">s3</span><span>=</span><span class="attribute-value">1</span><span>.2;  </span>
</li>
<li class="">
<span>if($L</span><span class="tag">></span><span>$s1){  </span>
</li>
<li class="alt"><span>//计算叶子的定位上面  </span></li>
<li class="">
<span>$</span><span class="attribute">x2</span><span>=$x+$L*cos($a*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y2</span><span>=$y+$L*sin($a*PII);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">x2R</span><span>=$x2+$L/$s2*cos(($a+$B)*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y2R</span><span>=$y2+$L/$s2*sin(($a+$B)*PII);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">x2L</span><span>=$x2+$L/$s2*cos(($a-$B)*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y2L</span><span>=$y2+$L/$s2*sin(($a-$B)*PII);  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>//计算叶子的定位下面  </span></li>
<li class="">
<span>$</span><span class="attribute">x1</span><span>=$x+$L/$s2*cos($a*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y1</span><span>=$y+$L/$s2*sin($a*PII);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">x1L</span><span>=$x1+$L/$s2*cos(($a-$B)*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y1L</span><span>=$y1+$L/$s2*sin(($a-$B)*PII);  </span>
</li>
<li class="">
<span>$</span><span class="attribute">x1R</span><span>=$x1+$L/$s2*cos(($a+$B)*PII);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">y1R</span><span>=$y1+$L/$s2*sin(($a+$B)*PII);  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>//别分画叶子的主干以及叶面  </span></li>
<li class=""><span>ImageLine($im,(int)$x,(int)$y,(int)$x2,(int)$y2,$g);  </span></li>
<li class="alt"><span>ImageLine($im,(int)$x2,(int)$y2,(int)$x2R,(int)$y2R,$g);  </span></li>
<li class=""><span>ImageLine($im,(int)$x2,(int)$y2,(int)$x2L,(int)$y2L,$g);  </span></li>
<li class="alt"><span>ImageLine($im,(int)$x1,(int)$y1,(int)$x1L,(int)$y1L,$g);  </span></li>
<li class=""><span>ImageLine($im,(int)$x1,(int)$y1,(int)$x1R,(int)$y1R,$g);  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>//再次递归调用本身  </span></li>
<li class="alt"><span>drawLeaf($g,$x2,$y2,$L/$s3,$a+$C);  </span></li>
<li class=""><span>drawLeaf($g,$x2R,$y2R,$L/$s2,$a+$B);  </span></li>
<li class="alt"><span>drawLeaf($g,$x2L,$y2L,$L/$s2,$a-$B);  </span></li>
<li class=""><span>drawLeaf($g,$x1L,$y1L,$L/$s2,$a-$B);  </span></li>
<li class="alt"><span>drawLeaf($g,$x1R,$y1R,$L/$s2,$a+$B);  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//实例化  </span></li>
<li class=""><span>drawLeaf($g,300,500,100,270);  </span></li>
<li class="alt"><span>header("Content-type:image/png");  </span></li>
<li class=""><span>imagepng($im);  </span></li>
<li class="alt">
<span></span><span class="tag">?></span><span>在我个人的PHP编程经验中,递归调用常常与静态变量使用。静态变量的含义可以参考PHP手册。希望下面的代码,会更有利于对递归以及静态变量的理解  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>header("Content-type:text/plain");  </span></li>
<li class=""><span>functionstatic_function(){  </span></li>
<li class="alt">
<span>static$</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;  </span>
</li>
<li class="">
<span>if($i++</span><span class="tag"><span class="tag-name">10</span><span>){  </span></span>
</li>
<li class="alt"><span>echo$i."n";  </span></li>
<li class=""><span>static_function();  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>static_function(); </span></li>
</ol>
Copy after login

这段代码会如数输出1到10的数字。在static_function函数第二次运行时,变量i由于是静态变量,所以仍被保留不被释放,进而可以得到自增的值。


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template