﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-linux&amp;c++  R&amp;D-随笔分类-Algorithm and Data Structure</title><link>http://www.cppblog.com/uglystone/category/4136.html</link><description>programing is a pleasure!</description><language>zh-cn</language><lastBuildDate>Fri, 23 May 2008 03:09:09 GMT</lastBuildDate><pubDate>Fri, 23 May 2008 03:09:09 GMT</pubDate><ttl>60</ttl><item><title>Thinking recursively</title><link>http://www.cppblog.com/uglystone/archive/2008/03/16/44620.html</link><dc:creator>丑石</dc:creator><author>丑石</author><pubDate>Sun, 16 Mar 2008 10:54:00 GMT</pubDate><guid>http://www.cppblog.com/uglystone/archive/2008/03/16/44620.html</guid><wfw:comment>http://www.cppblog.com/uglystone/comments/44620.html</wfw:comment><comments>http://www.cppblog.com/uglystone/archive/2008/03/16/44620.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/uglystone/comments/commentRss/44620.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/uglystone/services/trackbacks/44620.html</trackback:ping><description><![CDATA[<p>First,Let's know&nbsp;the principle:<br>Recursive leap of faith-<br>When you try to understand a recursive program,you must be able to put the underlying details aside and focus instead&nbsp;on a single level of the operation. At that level,you are allowed to assume that any recursive call automatically gets the right answer as long as the arguments to that call are simpler than the original arguments in some respect.The psychological strategy-assuming that any simpler recursive call will work correctly-is called the recursive leap of faith!<br>The idea may be difficult to newers! Take an example for it:<br>We all know the Fibonacci function:<br>F(n)=F(n-1)+F(n-2)<br>Recursive implementation of the Fibonacci funtion:<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id=Codehighlighter1_14_74_Open_Image onclick="this.style.display='none'; Codehighlighter1_14_74_Open_Text.style.display='none'; Codehighlighter1_14_74_Closed_Image.style.display='inline'; Codehighlighter1_14_74_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_14_74_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_14_74_Closed_Text.style.display='none'; Codehighlighter1_14_74_Open_Image.style.display='inline'; Codehighlighter1_14_74_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;Fib(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)</span><span id=Codehighlighter1_14_74_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_14_74_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(n</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;n;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;Fib(n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">Fib(n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p>&nbsp;if n is 5,Fib(5) is computed by the sum of Fib(4) and Fib(3).<br>Applying the faith,you can assume that&nbsp; the program correctly computes each of these values,without going through all the steps that Fib(4) and Fib(3) is computed!<br></p>
<img src ="http://www.cppblog.com/uglystone/aggbug/44620.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/uglystone/" target="_blank">丑石</a> 2008-03-16 18:54 <a href="http://www.cppblog.com/uglystone/archive/2008/03/16/44620.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>