﻿<?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++博客-flyinghearts-随笔分类-《编程之美》读书笔记</title><link>http://www.cppblog.com/flyinghearts/category/14112.html</link><description>http://www.cnblogs.com/flyinghearts</description><language>zh-cn</language><lastBuildDate>Wed, 20 Jul 2011 15:59:03 GMT</lastBuildDate><pubDate>Wed, 20 Jul 2011 15:59:03 GMT</pubDate><ttl>60</ttl><item><title>对环状数组求连续子数组的最大和</title><link>http://www.cppblog.com/flyinghearts/archive/2011/07/20/151513.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Wed, 20 Jul 2011 15:49:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2011/07/20/151513.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/151513.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2011/07/20/151513.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/151513.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/151513.html</trackback:ping><description><![CDATA[<br /><div>  <p align="left">&nbsp;<div>思路：由于数组元素总和是固定值，因而跨头、尾的连续子数组和最大，等价于求 中间那段和最小。因此，问题转为计算 连续子数组的最大和 与 连续子数组的最小和 </div></p><p align="left"><br /></p>  <p align="left"><strong><span style="font-family: 宋体; color: blue;">import</span></strong><span style="font-family: 宋体; color: black;"> std</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">algorithm</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong></p>  <p align="left">&nbsp;</p>  <p align="left"><strong><span style="font-family: 宋体; color: blue;">int</span></strong><span style="font-family: 宋体; color: black;"> ring_max_continue_sum</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><strong><span style="font-family: 宋体; color: blue;">in</span></strong> <strong><span style="font-family: 宋体; color: blue;">int</span></strong><strong><span style="font-family: 宋体; color: navy;">[]</span></strong><span style="font-family: 宋体; color: black;"> src</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">{</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span><strong><span style="font-family: 宋体; color: blue;">int</span></strong><span style="font-family: 宋体; color: black;"> min_sum </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> src</span><strong><span style="font-family: 宋体; color: navy;">[</span></strong><span style="font-family: 宋体; color: #ff8000;">0</span><strong><span style="font-family: 宋体; color: navy;">],</span></strong><span style="font-family: 宋体; color: black;"> cur_min_sum </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> min_sum</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span><strong><span style="font-family: 宋体; color: blue;">int</span></strong><span style="font-family: 宋体; color: black;"> max_sum </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> src</span><strong><span style="font-family: 宋体; color: navy;">[</span></strong><span style="font-family: 宋体; color: #ff8000;">0</span><strong><span style="font-family: 宋体; color: navy;">],</span></strong><span style="font-family: 宋体; color: black;"> cur_max_sum </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> max_sum</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span><strong><span style="font-family: 宋体; color: blue;">int</span></strong><span style="font-family: 宋体; color: black;"> total_sum </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> src</span><strong><span style="font-family: 宋体; color: navy;">[</span></strong><span style="font-family: 宋体; color: #ff8000;">0</span><strong><span style="font-family: 宋体; color: navy;">];</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span><strong><span style="font-family: 宋体; color: blue;">foreach</span></strong> <strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">value</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong><span style="font-family: 宋体; color: black;"> src</span><strong><span style="font-family: 宋体; color: navy;">[</span></strong><span style="font-family: 宋体; color: #ff8000;">1</span> <strong><span style="font-family: 宋体; color: navy;">..</span></strong><span style="font-family: 宋体; color: black;"> $</span><strong><span style="font-family: 宋体; color: navy;">])</span></strong> <strong><span style="font-family: 宋体; color: navy;">{</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;"><span>&nbsp;&nbsp;&nbsp; </span>cur_min_sum </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> min</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">value</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> cur_min_sum </span><strong><span style="font-family: 宋体; color: navy;">+</span></strong><span style="font-family: 宋体; color: black;"> value</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;"><span>&nbsp;&nbsp;&nbsp; </span>min_sum </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> min</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">min_sum</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> cur_min_sum</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;&nbsp; </span></p>  <p align="left"><span style="font-family: 宋体; color: black;"><span>&nbsp;&nbsp;&nbsp; </span>cur_max_sum </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> max</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">value</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> cur_max_sum </span><strong><span style="font-family: 宋体; color: navy;">+</span></strong><span style="font-family: 宋体; color: black;"> value</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;"><span>&nbsp;&nbsp;&nbsp; </span>max_sum </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> max</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">max_sum</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> cur_max_sum</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;&nbsp; </span></p>  <p align="left"><span style="font-family: 宋体; color: black;"><span>&nbsp;&nbsp;&nbsp; </span>total_sum </span><strong><span style="font-family: 宋体; color: navy;">+=</span></strong><span style="font-family: 宋体; color: black;"> value</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span><strong><span style="font-family: 宋体; color: navy;">}</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span><strong><span style="font-family: 宋体; color: blue;">return</span></strong><span style="font-family: 宋体; color: black;"> max</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">max_sum</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> total_sum </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> min_sum</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">}</span></strong></p>  <p align="left">&nbsp;</p>  <p align="left"><strong><span style="font-family: 宋体; color: blue;">unittest</span></strong> <strong><span style="font-family: 宋体; color: navy;">{</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span><strong><span style="font-family: 宋体; color: blue;">auto</span></strong><span style="font-family: 宋体; color: black;"> ta </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong> <strong><span style="font-family: 宋体; color: navy;">[</span></strong><span style="font-family: 宋体; color: #ff8000;">3</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: #ff8000;">2</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #ff8000;">3</span><strong><span style="font-family: 宋体; color: navy;">];</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span><strong><span style="font-family: 宋体; color: blue;">auto</span></strong><span style="font-family: 宋体; color: black;"> tb </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong> <strong><span style="font-family: 宋体; color: navy;">[</span></strong><span style="font-family: 宋体; color: #ff8000;">3</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #ff8000;">4</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: #ff8000;">2</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #ff8000;">3</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: #ff8000;">7</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #ff8000;">1</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: #ff8000;">3</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #ff8000;">8</span><strong><span style="font-family: 宋体; color: navy;">];</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span><strong><span style="font-family: 宋体; color: blue;">assert</span></strong><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">ta</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">ring_max_continue_sum </span><strong><span style="font-family: 宋体; color: navy;">==</span></strong> <span style="font-family: 宋体; color: #ff8000;">6</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;</span><strong><span style="font-family: 宋体; color: blue;">assert</span></strong><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">tb</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">ring_max_continue_sum </span><strong><span style="font-family: 宋体; color: navy;">==</span></strong> <span style="font-family: 宋体; color: #ff8000;">16</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong> </p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">}</span></strong></p>  <p align="left">&nbsp;</p>  <p align="left"><strong><span style="font-family: 宋体; color: blue;">void</span></strong><span style="font-family: 宋体; color: black;"> main</span><strong><span style="font-family: 宋体; color: navy;">()</span></strong></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">{</span></strong></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">}</span></strong></p>  <p>&nbsp;</p>  </div><img src ="http://www.cppblog.com/flyinghearts/aggbug/151513.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2011-07-20 23:49 <a href="http://www.cppblog.com/flyinghearts/archive/2011/07/20/151513.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最短摘要的生成（补充）</title><link>http://www.cppblog.com/flyinghearts/archive/2011/07/20/151511.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Wed, 20 Jul 2011 15:39:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2011/07/20/151511.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/151511.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2011/07/20/151511.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/151511.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/151511.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 把另一种解法也贴出来吧（由于题目的背景是在大量数据中查找少数关键字，效率不如前面的解法）。基本思路：将 关键字 从1开始编号， 对分词后源词组，进行映射，如果是关键字，就映射为该关键字的编号，否则映射为数字0用两个指针，一前一后，很容易就可以实现在O(n)时间内找出结果。Code highlighting produced by Actipro CodeHighlighter (freeware)...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2011/07/20/151511.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/151511.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2011-07-20 23:39 <a href="http://www.cppblog.com/flyinghearts/archive/2011/07/20/151511.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>点在三角形内（1）</title><link>http://www.cppblog.com/flyinghearts/archive/2011/07/07/150424.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Thu, 07 Jul 2011 15:16:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2011/07/07/150424.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/150424.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2011/07/07/150424.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/150424.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/150424.html</trackback:ping><description><![CDATA[<div><div id="cnblogs_post_body"><div><strong><span style="font-family: 宋体;">问题：判断点</span>P</strong><strong><span style="font-family: 宋体;">是否在三角形</span>ABC</strong><strong><span style="font-family: 宋体;">内</span></strong>  <p><br /></p>  <p style="text-indent: 21pt;"><span style="font-family: 宋体;">判断一个点是否在在三角形内，最常用的两种方法：面积法、向量同向法。算法虽然很简单，但要做到高效却不容易，要考虑到二维、三维的区别，还要考虑到坐标是用浮点数还是用整数来表示。</span></p>  <p style="text-indent: 21pt;">&nbsp;</p>  <p style="text-indent: 21pt;"><span style="font-family: 宋体;">在二维平面上，问题相对简单，一般只需</span>6<span style="font-family: 宋体;">次乘法计算。但在三维平面时问题要复杂很多，在网上看到的算法，一般都需要</span><strong><span style="color: blue;">30</span></strong><span style="font-family: 宋体;">次乘法计算（如果已知点</span>P<span style="font-family: 宋体;">在平面</span>ABC<span style="font-family: 宋体;">上，则需</span><strong><span style="color: blue;">21</span></strong><span style="font-family: 宋体;">次）。实际上，在三维坐标系下，可以做到增加</span>1<span style="font-family: 宋体;">次比较，将乘法计算降到</span><strong><span style="color: blue;">13</span></strong><span style="font-family: 宋体;">次（如果点</span>P<span style="font-family: 宋体;">在平面</span>ABC<span style="font-family: 宋体;">上，则最多只要</span><strong><span style="color: blue;">8</span></strong><span style="font-family: 宋体;">次乘法计算）。</span></p>  <p>&nbsp;</p>  <p style="text-indent: 21pt;"><span style="font-family: 宋体;">最常用的两种方法：面积法和向量同向法本质上是等价的。</span></p>  <p style="margin-left: 84pt; text-indent: -63pt;"><span style="font-family: 宋体;">向量同向法：若点</span>P<span style="font-family: 宋体;">在三角形内，则三个向量：</span>ab &#215; ap<span style="font-family: 宋体;">、</span>ap &#215; ac<span style="font-family: 宋体;">、</span>pb &#215; pc<span style="font-family: 宋体;">平行同向（它们也与向量</span>ab &#215; ac<span style="font-family: 宋体;">平行同向），由于这三个向量均有可能为</span>0<span style="font-family: 宋体;">，直接判断它们平行同向相当麻烦，但考虑到</span>ab &#215; ac<span style="font-family: 宋体;">不可能为</span>0<span style="font-family: 宋体;">，直接判断&#8220;<strong><span style="color: #993300;">向量：</span></strong></span><strong><span style="color: #993300;">ab &#215; ap</span></strong><strong><span style="font-family: 宋体; color: #993300;">、</span><span style="color: #993300;">ap &#215; ac</span></strong><strong><span style="font-family: 宋体; color: #993300;">、</span><span style="color: #993300;">pb &#215; pc</span></strong><strong><span style="font-family: 宋体; color: #993300;">均与</span><span style="color: #993300;">ab &#215; ac</span></strong><strong><span style="font-family: 宋体; color: #993300;">平行同向</span></strong><span style="font-family: 宋体;">&#8221;反而更简单。</span></p>  <p style="margin-left: 84pt; text-indent: -63pt;">&nbsp;</p>  <p style="text-indent: 21pt;"><span style="font-family: 宋体;">面积法：当点</span>p<span style="font-family: 宋体;">在三角形</span>abc<span style="font-family: 宋体;">内时，</span>4<span style="font-family: 宋体;">个三角形的面积满足：</span> abc&nbsp;= abp + apc + pbc</p>  <p style="text-indent: 63pt;"><span style="font-family: 宋体;">对面积的计算，可以通过向量的向量积计算得到：</span> <span style="font-family: 宋体;">面积</span> abc&nbsp;= |ab &#215; ac| / 2</p>  <p style="text-indent: 63pt;"><span style="font-family: 宋体;">表面上，要计算</span>4<span style="font-family: 宋体;">个三角形的面积，但根据下面的公式：</span></p>  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ap &#215; ap = 0,&nbsp;<strong><span style="color: #993300;">pb &#215; pc = (ab - ap) &#215; (ac - ap) = ab &#215; ac - ab &#215; ap - ap &#215; ac</span></strong></p>  <p style="text-indent: 63.25pt;"><strong><span style="font-family: 宋体; color: #993300;">可以少算一次矢量积</span></strong><span style="font-family: 宋体;">。</span></p>  <p style="text-indent: 57.75pt;"><span style="font-family: 宋体;">公式：</span> <span style="color: blue;">|ab &#215; ac| = |ab &#215; ap| + |ap &#215; ac| + |(ab &#215; ac - ab &#215; ap - ap &#215; ac)| </span></p>  <p style="text-indent: 57.75pt;">&nbsp;</p>  <p style="text-indent: 21pt;"><span style="font-family: 宋体;">对任意向量</span>a<span style="font-family: 宋体;">、</span>b<span style="font-family: 宋体;">、</span>c<span style="font-family: 宋体;">：</span><span>&nbsp;&nbsp; |a + b + c| = |a| + |b| + |c|&nbsp;&lt;==&gt;&nbsp;</span><span style="font-family: 宋体;">向量</span>a<span style="font-family: 宋体;">、</span>b<span style="font-family: 宋体;">、</span>c <span style="font-family: 宋体;">平行同向</span></p>  <p>&nbsp;&nbsp;&nbsp;<span style="font-family: 宋体;">因而，面积法和向量同向法本质上是等价的。</span></p>  <p>&nbsp;</p>  <p>&nbsp;</p>  <p style="text-indent: 15.75pt;"><span style="font-family: 宋体;">下面先讨论二维坐标系（每个点</span>X<span style="font-family: 宋体;">，都看作是原点</span>O<span style="font-family: 宋体;">到该点</span>X<span style="font-family: 宋体;">的二维向量</span>OX<span style="font-family: 宋体;">）。</span></p>  <p style="text-indent: 15.75pt;"><span style="font-family: 宋体;">先定义一个二维向量模板：</span></p>  <p>&nbsp;</p>  <p align="left"><span style="font-family: 宋体; color: #8000ff;">template</span><strong><span style="font-family: 宋体; color: navy;">&lt;</span></strong><span style="font-family: 宋体; color: #8000ff;">typename</span><span style="font-family: 宋体; color: black;"> T</span><strong><span style="font-family: 宋体; color: navy;">&gt;</span></strong> <span style="font-family: 宋体; color: #8000ff;">class</span><span style="font-family: 宋体; color: black;"> Vec2 </span><strong><span style="font-family: 宋体; color: navy;">{</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;T x</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> y</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: #8000ff;">public</span><strong><span style="font-family: 宋体; color: navy;">:</span></strong></p>  <p align="left">&nbsp;<strong><span style="font-family: 宋体; color: blue;">typedef</span></strong><span style="font-family: 宋体; color: black;"> T value_type</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;Vec2</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">T xx </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong> <span style="font-family: 宋体; color: #ff8040;">0</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> T yy </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong> <span style="font-family: 宋体; color: #ff8040;">0</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">:</span></strong><span style="font-family: 宋体; color: black;"> x</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">xx</span><strong><span style="font-family: 宋体; color: navy;">),</span></strong><span style="font-family: 宋体; color: black;"> y</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">yy</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">{};</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;T cross</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vec2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> v</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span> <strong><span style="font-family: 宋体; color: navy;">{</span></strong> <strong><span style="font-family: 宋体; color: blue;">return</span></strong><span style="font-family: 宋体; color: black;"> x </span><strong><span style="font-family: 宋体; color: navy;">*</span></strong><span style="font-family: 宋体; color: black;"> v</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">y </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> y </span><strong><span style="font-family: 宋体; color: navy;">*</span></strong><span style="font-family: 宋体; color: black;"> v</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">x</span><strong><span style="font-family: 宋体; color: navy;">;}</span></strong>&nbsp;<span style="font-family: 宋体; color: green;">// </span><span style="font-family: 宋体; color: green;">矢量积</span></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;Vec2 </span><strong><span style="font-family: 宋体; color: blue;">operator</span></strong><strong><span style="font-family: 宋体; color: navy;">-(</span></strong><span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vec2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> v</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span> <strong><span style="font-family: 宋体; color: navy;">{</span></strong> <strong><span style="font-family: 宋体; color: blue;">return</span></strong><span style="font-family: 宋体; color: black;"> Vec2</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">x </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> v</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">x</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> y </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> v</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">y</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong> <strong><span style="font-family: 宋体; color: navy;">}</span></strong></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">};</span></strong></p>  <p>&nbsp;</p>  <p>&nbsp;</p>  <p style="text-indent: 21pt;"><span style="font-family: 宋体;">如果坐标采用浮点数，考虑到浮点数取绝对值方便（有专门的浮点指令），但彼此间比较大小存在误差，采用面积法比较方便：</span></p>    <p align="left">&nbsp;</p>  <p align="left"><strong><span style="font-family: 宋体; color: blue;">typedef</span></strong><span style="font-family: 宋体; color: black;"> Vec2</span><strong><span style="font-family: 宋体; color: navy;">&lt;</span></strong><span style="font-family: 宋体; color: #8000ff;">double</span><strong><span style="font-family: 宋体; color: navy;">&gt;</span></strong><span style="font-family: 宋体; color: black;"> Vd2</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong></p>  <p align="left">&nbsp;</p>  <p align="left"><span style="font-family: 宋体; color: #8000ff;">bool</span><span style="font-family: 宋体; color: black;"> is_in_triangle</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vd2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> a</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vd2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> b</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vd2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> c</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vd2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> p</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">{</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;Vd2 ab</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">b </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;">a</span><strong><span style="font-family: 宋体; color: navy;">),</span></strong><span style="font-family: 宋体; color: black;"> ac</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">c </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> a</span><strong><span style="font-family: 宋体; color: navy;">),</span></strong><span style="font-family: 宋体; color: black;"> ap</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">p </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> a</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left">&nbsp;</p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: green;">//</span><span style="font-family: 宋体; color: green;">用矢量积计算面积，下面4个值的绝对值，是对应的三角形的面积的两倍，</span></p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">double</span><span style="font-family: 宋体; color: black;"> abc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> ab</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">cross</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">ac</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong> </p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">double</span><span style="font-family: 宋体; color: black;"> abp </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> ab</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">cross</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">ap</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">double</span><span style="font-family: 宋体; color: black;"> apc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> ap</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">cross</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">ac</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong> </p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">double</span><span style="font-family: 宋体; color: black;"> pbc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> abc </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> abp </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> apc</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong><span style="font-family: 宋体; color: black;">&nbsp;&nbsp; </span><span style="font-family: 宋体; color: green;">//</span><span style="font-family: 宋体; color: green;">等于pb.cross(pc)</span></p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: green;">//</span><span style="font-family: 宋体; color: green;">面积法：4个三角形的面积差 等于 0</span></p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">double</span><span style="font-family: 宋体; color: black;"> delta </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> fabs</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">abc</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> fabs</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">abp</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> fabs</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">apc</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> fabs</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">pbc</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong> </p>  <p align="left">&nbsp;<strong><span style="font-family: 宋体; color: blue;">return</span></strong><span style="font-family: 宋体; color: black;"> fabs</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">delta</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">&lt;</span></strong><span style="font-family: 宋体; color: black;"> DBL_EPSILON</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong><span style="font-family: 宋体; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">}</span></strong></p>  <p>&nbsp;</p>  <p>&nbsp;</p>  <p style="text-indent: 21pt;"><span style="font-family: 宋体;">如果坐标采用整数表示，代码相对麻烦点：</span></p>  <p style="text-indent: 21pt;">&nbsp;</p>  <p align="left"><strong><span style="font-family: 宋体; color: blue;">typedef</span></strong><span style="font-family: 宋体; color: black;"> Vec2</span><strong><span style="font-family: 宋体; color: navy;">&lt;</span></strong><span style="font-family: 宋体; color: #8000ff;">int</span><strong><span style="font-family: 宋体; color: navy;">&gt;</span></strong><span style="font-family: 宋体; color: black;"> Vi2</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong></p>  <p align="left">&nbsp;</p>  <p align="left"><span style="font-family: 宋体; color: #8000ff;">bool</span><span style="font-family: 宋体; color: black;"> is_in_triangle</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vi2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> a</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vi2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> b</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vi2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> c</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vi2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> p</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">{</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;Vi2 ab</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">b </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;">a</span><strong><span style="font-family: 宋体; color: navy;">),</span></strong><span style="font-family: 宋体; color: black;"> ac</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">c </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> a</span><strong><span style="font-family: 宋体; color: navy;">),</span></strong><span style="font-family: 宋体; color: black;"> ap</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">p </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> a</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left">&nbsp;</p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: green;">//</span><span style="font-family: 宋体; color: green;">用矢量积计算面积，下面4个值的绝对值，是对应的三角形的面积的两倍，</span></p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> abc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> ab</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">cross</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">ac</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong> </p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> abp </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> ab</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">cross</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">ap</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> apc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> ap</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">cross</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">ac</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong> </p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> pbc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> abc </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> abp </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> apc</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong><span style="font-family: 宋体; color: black;">&nbsp;&nbsp; </span><span style="font-family: 宋体; color: green;">//</span><span style="font-family: 宋体; color: green;">等于pb.cross(pc)</span></p>  <p align="left">&nbsp;</p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: green;">//</span><span style="font-family: 宋体; color: green;">方法1： 面积法：4个三角形的面积差 等于 0</span></p>  <p align="left">&nbsp;<strong><span style="font-family: 宋体; color: blue;">return</span></strong><span style="font-family: 宋体; color: black;"> abs</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">abc</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">==</span></strong><span style="font-family: 宋体; color: black;"> abs</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">abp</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">+</span></strong><span style="font-family: 宋体; color: black;"> abs</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">apc</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">+</span></strong><span style="font-family: 宋体; color: black;"> abs</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">pbc</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong></p>  <p align="left">&nbsp;</p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: green;">//</span><span style="font-family: 宋体; color: green;">方法2： 矢量同向法： abp apc pbc 均与 abc 同向：</span></p>  <p align="left">&nbsp;<strong><span style="font-family: 宋体; color: blue;">if</span></strong> <strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">abc </span><strong><span style="font-family: 宋体; color: navy;">&lt;</span></strong> <span style="font-family: 宋体; color: #ff8040;">0</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">{</span></strong><span style="font-family: 宋体; color: black;"> abp </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;">abp</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong><span style="font-family: 宋体; color: black;"> apc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;">apc</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong><span style="font-family: 宋体; color: black;"> pbc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;">pbc</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong> <strong><span style="font-family: 宋体; color: navy;">}</span></strong></p>  <p align="left">&nbsp;<strong><span style="font-family: 宋体; color: blue;">return</span></strong> <strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">abp </span><strong><span style="font-family: 宋体; color: navy;">&gt;=</span></strong> <span style="font-family: 宋体; color: #ff8040;">0</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong> <strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">apc </span><strong><span style="font-family: 宋体; color: navy;">&gt;=</span></strong> <span style="font-family: 宋体; color: #ff8040;">0</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong> <strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">pbc </span><strong><span style="font-family: 宋体; color: navy;">&gt;=</span></strong> <span style="font-family: 宋体; color: #ff8040;">0</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong> </p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">}</span></strong></p>    <p align="left">&nbsp;</p>  <p style="margin-left: 36.75pt; text-align: left; text-indent: -36.75pt;" align="left"><span style="font-family: 宋体; color: black;">方法1：要计算4次绝对值，看似需要4次条件跳转，但主流的编译器，都能采用</span><span style="font-family: 宋体;">位运算直接计算绝对值（<strong>注意：</strong></span><strong>GCC</strong><strong><span style="font-family: 宋体;">需要加额外的参数</span></strong><span style="font-family: 宋体;">），不需要任何条件跳转。</span></p>  <p align="left"><span style="font-family: 宋体; color: black;">方法2：比方法1指令少，但多1次条件跳转。</span></p>  <p><span style="font-family: 宋体; color: black;">哪种方法效率较高，与编译器生成的具体代码有关。</span></p>  <p>&nbsp;</p>  <p><span style="font-family: 宋体;">上面代码中，可采用的两种优化方法：</span></p>  <p><span style="font-family: 宋体;">&#9312;　对整数</span>x<span style="font-family: 宋体;">取绝对值，可以利用位运算：</span></p>  <p>&nbsp;&nbsp;&nbsp; <span style="font-family: 宋体;">设</span>&nbsp;y = 0 <span style="font-family: 宋体;">（当</span>x &gt;= 0<span style="font-family: 宋体;">）</span></p>  <p>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;= -1 <span style="font-family: 宋体;">（当</span>x &lt; 0<span style="font-family: 宋体;">）</span> </p>  <p>&nbsp;<span style="font-family: 宋体;">（编译器可以利用</span>cdq<span style="font-family: 宋体;">或</span>sar<span style="font-family: 宋体;">等指令直接由</span>x<span style="font-family: 宋体;">计算出</span>y<span style="font-family: 宋体;">值）</span></p>  <p>&nbsp;<span style="font-family: 宋体;">则</span>&nbsp;<strong>abs(x) &nbsp;= &nbsp;(x xor y) &#8211; y</strong></p>  <p><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family: 宋体;">或：</span>&nbsp;&nbsp; <strong>=&nbsp;(x + y) xor y</strong></p>  <p><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family: 宋体;">或：</span>&nbsp;&nbsp; <strong>=&nbsp;x &#8211; (2 * x &amp; y)</strong></p>  <p>&nbsp;</p>  <p>&nbsp;<span style="font-family: 宋体;">&#9313;</span> <span style="font-family: 宋体;">对整数</span>a<span style="font-family: 宋体;">、</span>b<span style="font-family: 宋体;">、</span>c<span style="font-family: 宋体;">，</span>&nbsp;<strong>a &gt;= 0 &amp;&amp; b &gt;= 0 &amp;&amp; c &gt;= 0</strong> <span style="font-family: 宋体;">等价于</span> </p>  <p style="text-indent: 21.1pt;"><strong>(a &gt;= 0) &amp; (b &gt;= 0) &amp; (c &gt;= 0) </strong><span style="font-family: 宋体;">等价于：</span></p>  <p style="text-indent: 21.1pt;"><strong>(a | b | c) &gt;= 0</strong></p>  <p align="left">&nbsp;</p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: black;">为避免编译器没有进行相关优化，直接手动优化，可得：</span></p>  <p align="left">&nbsp;</p>  <p align="left"><span style="font-family: 宋体; color: #8000ff;">inline</span> <span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> chg_sign</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> x</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> sign</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <span style="font-family: 宋体; color: green;">//sign</span><span style="font-family: 宋体; color: green;">只能取0或-1，函数分别返回x、-x</span></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">{</span></strong></p>  <p align="left">&nbsp;<strong><span style="font-family: 宋体; color: blue;">return</span></strong> <strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">x </span><strong><span style="font-family: 宋体; color: navy;">+</span></strong><span style="font-family: 宋体; color: black;"> sign</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">^</span></strong><span style="font-family: 宋体; color: black;"> sign</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong> </p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: green;">//return (x ^ sign) - sign; </span></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">}</span></strong></p>  <p align="left">&nbsp;</p>  <p align="left"><span style="font-family: 宋体; color: #8000ff;">bool</span><span style="font-family: 宋体; color: black;"> is_in_triangle</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vi2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> a</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vi2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> b</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vi2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> c</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong> <span style="font-family: 宋体; color: #8000ff;">const</span><span style="font-family: 宋体; color: black;"> Vi2</span><strong><span style="font-family: 宋体; color: navy;">&amp;</span></strong><span style="font-family: 宋体; color: black;"> p</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">{</span></strong></p>  <p align="left"><span style="font-family: 宋体; color: black;">&nbsp;Vi2 ab</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">b </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;">a</span><strong><span style="font-family: 宋体; color: navy;">),</span></strong><span style="font-family: 宋体; color: black;"> ac</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">c </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> a</span><strong><span style="font-family: 宋体; color: navy;">),</span></strong><span style="font-family: 宋体; color: black;"> ap</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">p </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> a</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left">&nbsp;</p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: green;">//</span><span style="font-family: 宋体; color: green;">用矢量积计算面积，下面4个值的绝对值，是对应的三角形的面积的两倍，</span></p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> abc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> ab</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">cross</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">ac</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong> </p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> abp </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> ab</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">cross</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">ap</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong></p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> apc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> ap</span><strong><span style="font-family: 宋体; color: navy;">.</span></strong><span style="font-family: 宋体; color: black;">cross</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">ac</span><strong><span style="font-family: 宋体; color: navy;">);</span></strong> </p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> pbc </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong><span style="font-family: 宋体; color: black;"> abc </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> abp </span><strong><span style="font-family: 宋体; color: navy;">-</span></strong><span style="font-family: 宋体; color: black;"> apc</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong><span style="font-family: 宋体; color: black;">&nbsp;&nbsp; </span><span style="font-family: 宋体; color: green;">//</span><span style="font-family: 宋体; color: green;">等于pb.cross(pc)</span></p>  <p align="left">&nbsp;</p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: green;">//</span><span style="font-family: 宋体; color: green;">方法3： 矢量同向法（优化版）</span></p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: #8000ff;">const</span> <span style="font-family: 宋体; color: #8000ff;">int</span><span style="font-family: 宋体; color: black;"> sign </span><strong><span style="font-family: 宋体; color: navy;">=</span></strong> <strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">abc </span><strong><span style="font-family: 宋体; color: navy;">&gt;=</span></strong> <span style="font-family: 宋体; color: #ff8040;">0</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">-</span></strong> <span style="font-family: 宋体; color: #ff8040;">1</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong> </p>  <p align="left">&nbsp;<span style="font-family: 宋体; color: green;">//const int sign = abc &gt;&gt; (sizeof(abc) * CHAR_BIT - 1);</span></p>  <p align="left">&nbsp;<strong><span style="font-family: 宋体; color: blue;">return</span></strong> <strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">chg_sign</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">abp</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> sign</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">|</span></strong><span style="font-family: 宋体; color: black;"> chg_sign</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">apc</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> sign</span><strong><span style="font-family: 宋体; color: navy;">)</span></strong> <strong><span style="font-family: 宋体; color: navy;">|</span></strong><span style="font-family: 宋体; color: black;"> chg_sign</span><strong><span style="font-family: 宋体; color: navy;">(</span></strong><span style="font-family: 宋体; color: black;">pbc</span><strong><span style="font-family: 宋体; color: navy;">,</span></strong><span style="font-family: 宋体; color: black;"> sign</span><strong><span style="font-family: 宋体; color: navy;">))</span></strong> <strong><span style="font-family: 宋体; color: navy;">&gt;=</span></strong> <span style="font-family: 宋体; color: #ff8040;">0</span><strong><span style="font-family: 宋体; color: navy;">;</span></strong></p>  <p align="left"><strong><span style="font-family: 宋体; color: navy;">}</span></strong></p>  <p>&nbsp;</p>  </div></div><br />作者：<a href="http://www.cnblogs.com/flyinghearts/"> flyinghearts </a><br />出处：<a href="http://www.cnblogs.com/flyinghearts/"> http://www.cnblogs.com/flyinghearts/ </a><br />本文采用<a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/">知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议</a>进行许可，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</div><img src ="http://www.cppblog.com/flyinghearts/aggbug/150424.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2011-07-07 23:16 <a href="http://www.cppblog.com/flyinghearts/archive/2011/07/07/150424.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记25：  2.21只考加法的面试题</title><link>http://www.cppblog.com/flyinghearts/archive/2011/03/27/142814.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 27 Mar 2011 15:09:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2011/03/27/142814.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/142814.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2011/03/27/142814.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/142814.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/142814.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 《编程之美》读书笔记25：&nbsp; 2.21只考加法的面试题&nbsp;我们知道:1+2 = 3；4+5 = 9；2+3+4 = 9。等式的左边都是两个或两个以上连续的自然数相加，是不是所有的整数都可以写成这样的形式呢？问题1：&nbsp; 对于一个64位正整数，输出它所有可能的连续自然数（两个以上）之和的算式。问题2：&nbsp; 大家在测试上面程序的过程中，...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2011/03/27/142814.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/142814.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2011-03-27 23:09 <a href="http://www.cppblog.com/flyinghearts/archive/2011/03/27/142814.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记24：  3.5 最短摘要的生成</title><link>http://www.cppblog.com/flyinghearts/archive/2011/03/27/142811.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 27 Mar 2011 14:11:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2011/03/27/142811.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/142811.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2011/03/27/142811.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/142811.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/142811.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 《编程之美》读书笔记24：&nbsp; 3.5 最短摘要的生成当初看这道题时，看了好了几遍都没看懂。后来总算弄明白：给出的字符串是用其它程序分好词的，关键字符串也是用其它程序分好词的，而不是按用户直接输入的字符串。比如书上给的例子：&#8220;微软亚洲研究院&nbsp; 使命&#8221;，不是按空格分成两个关键词，&#8220;微软亚洲研究院&#8221;和&#8220;使命&#822...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2011/03/27/142811.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/142811.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2011-03-27 22:11 <a href="http://www.cppblog.com/flyinghearts/archive/2011/03/27/142811.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记23： 1.1    让CPU占用率曲线听你指挥</title><link>http://www.cppblog.com/flyinghearts/archive/2010/12/02/135311.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Thu, 02 Dec 2010 15:19:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/12/02/135311.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/135311.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/12/02/135311.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/135311.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/135311.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 题目：写一个程序，让用户来决定Windows任务管理器（Task Manager）的CPU占用率。程序越精简越好，计算机语言不限。例如，可以实现下面三种情况：&nbsp;1.&nbsp;&nbsp;&nbsp; CPU的占用率固定在50%，为一条直线； 2.&nbsp;&nbsp;&nbsp; CPU的占用率为一条直线，但是具体占用率由命令行参数决定（参数范围1~ 100）； 3....&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2010/12/02/135311.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/135311.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-12-02 23:19 <a href="http://www.cppblog.com/flyinghearts/archive/2010/12/02/135311.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记 目录</title><link>http://www.cppblog.com/flyinghearts/archive/2010/09/01/125550.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Wed, 01 Sep 2010 11:32:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/09/01/125550.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/125550.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/09/01/125550.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/125550.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/125550.html</trackback:ping><description><![CDATA[<a id=_47b48872414a_HomePageDays_ctl00_DayList_ctl02_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/06/23/118595.html"><br></a><span style="FONT-SIZE: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;目&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 录<br><br><a id=viewpost1_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/12/02/135311.html"><font color=#000080>《编程之美》读书笔记23： 1.1&nbsp;&nbsp;&nbsp; 让CPU占用率曲线听你指挥</font></a><br></span><a id=_47b48872414a_HomePageDays_DaysList_ctl01_DayItem_DayList_ctl04_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123533.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记01： 1.2&nbsp;&nbsp;&nbsp;&nbsp;中国象棋将帅问题</span></a><a id=_47b48872414a_HomePageDays_ctl00_DayList_ctl02_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/06/23/118595.html"><span style="FONT-SIZE: 12pt"> <br>《编程之美》读书笔记02： 1.3&nbsp;&nbsp;&nbsp;&nbsp;一摞烙饼的排序</span></a><span style="FONT-SIZE: 12pt">*<br></span><a id=_47b48872414a_HomePageDays_DaysList_ctl01_DayItem_DayList_ctl03_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123534.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记03： 1.4&nbsp;&nbsp;&nbsp;&nbsp;买书问题*<br></span></a><a id=_47b48872414a_HomePageDays_DaysList_ctl01_DayItem_DayList_ctl02_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123535.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记04： 1.8&nbsp;&nbsp;&nbsp;&nbsp;小飞的电梯调度算法</span></a><a id=_47b48872414a_HomePageDays_DaysList_ctl01_DayItem_DayList_ctl03_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123534.html"><br></a><a id=_47b48872414a_HomePageDays_DaysList_ctl01_DayItem_DayList_ctl01_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123536.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记05： 1.9&nbsp;&nbsp;&nbsp;&nbsp;高效率的安排见面会</span></a><br><a id=_47b48872414a_HomePageDays_DaysList_ctl01_DayItem_DayList_ctl00_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123537.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记06： 1.13&nbsp;&nbsp;NIM(3)两堆石头的游戏<br></a><a id=_1fe0cc53df77_HomePageDays_ctl00_DayList_ctl01_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/01/121907.html"><font color=#000080>《编程之美》读书笔记22： 1.16&nbsp; 24点游戏</font></a>*<a id=_47b48872414a_HomePageDays_DaysList_ctl01_DayItem_DayList_ctl00_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123537.html"><font color=#000000> </font><br></span></a><a id=PostsList1_rpPosts_ctl17_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123531.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记22： 1.16&nbsp;&nbsp;24点游戏（补充）</span></a><a id=_47b48872414a_HomePageDays_DaysList_ctl01_DayItem_DayList_ctl00_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123537.html"><br></a><a id=_47b48872414a_HomePageDays_ctl00_DayList_ctl00_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/07/21/120915.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记21： 2.4&nbsp;&nbsp;&nbsp;&nbsp;1的数目</span></a><span style="FONT-SIZE: 12pt">*<br></span><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl11_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123538.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记07： 2.5&nbsp;&nbsp;&nbsp;&nbsp;寻找最大的K个数</span></a><span style="FONT-SIZE: 12pt">*<br></span><a id=PostsList1_rpPosts_ctl23_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/06/23/118593.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记08： 2.9&nbsp;&nbsp;&nbsp; Fibonacci序列 —— O(log n)求Fibonacci数列（非矩阵法）</span></a><span style="FONT-SIZE: 12pt">&nbsp;*<br></span><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl01_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123549.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记17： 2.16&nbsp; 求数组中最长递增子序列</span></a><br><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl10_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123539.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记09： 2.17 &nbsp;数组循环移位</span></a><br><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl09_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123541.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记10： 2.18&nbsp; 数组分割</span></a><span style="FONT-SIZE: 12pt"> <br></span><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl08_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123542.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记11： 3.3&nbsp;&nbsp;&nbsp; 计算字符串的相似度</span></a><br><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl00_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123550.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记18： 3.7&nbsp;&nbsp;&nbsp; 队列中取最大数操作问题</span></a><br><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl07_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123543.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记12： 3.8&nbsp;&nbsp;&nbsp; 求二叉树中节点的最大距离</span></a><span style="FONT-SIZE: 12pt">* <br></span><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl06_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123544.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记19： 3.9&nbsp;&nbsp; &nbsp;重建二叉树</span></a><br><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl02_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123548.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记16： 3.10&nbsp; 分层遍历二叉树</span></a><br><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl05_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123545.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记13： 4.1&nbsp;&nbsp;&nbsp; 金刚坐飞机问题</span></a><span style="FONT-SIZE: 12pt">*<br></span><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl04_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123546.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记14： 4.4&nbsp;&nbsp; &nbsp;是否在三角形内</span></a><br><a id=_47b48872414a_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl03_TitleUrl href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123547.html"><span style="FONT-SIZE: 12pt">《编程之美》读书笔记15： 4.5&nbsp;&nbsp; &nbsp;磁带文件存放优化</span></a><span style="FONT-SIZE: 12pt">*</span> 
<img src ="http://www.cppblog.com/flyinghearts/aggbug/125550.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-09-01 19:32 <a href="http://www.cppblog.com/flyinghearts/archive/2010/09/01/125550.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记18： 3.7 队列中取最大数操作问题</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123550.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:43:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123550.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123550.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123550.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123550.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123550.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 《编程之美》读书笔记18： 3.7 队列中取最大数操作问题&nbsp;若不使用C++新标准的右值引用，DeQueue的实现是低效的，因为要返回的元素只能通过赋值操作，而不能通过引用。（书上的实现代码，竟然少了对EnQueue的实现！）思路：用一个辅助队列来记录最大元素（为节省空间，只记录其地址），当有一个元素入队，就将辅助队列尾端不大于该元素的全部出队后（注意相等的也要出队），再将该元...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2010/08/16/123550.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/123550.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:43 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123550.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记17： 2.16 求数组中最长递增子序列</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123549.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:39:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123549.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123549.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123549.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123549.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123549.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>17</span>： <span lang=EN-US>2.16 </span>求数组中最长递增子序列<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">思路：每处理一个数，都可以将这个数插入到已经找到的某个递增子序列（假设包含无限个长度为<span lang=EN-US>0</span>的空序列）后，使其长度增加<span lang=EN-US>1</span>，处理完毕后，这些长度最大值即为所求。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">具有相同长度<span lang=EN-US>i+1</span>的递增子序列，若这些序列的最后一个数最小值为<span lang=EN-US>min_v[i]</span>，其所在的序列为<span lang=EN-US>A</span>，则若某个数能插入到序列<span lang=EN-US>A</span>后面，必然能插入到其它相同长度的递增子序列后面，而能否插入到序列<span lang=EN-US>A</span>后面，仅由<span lang=EN-US>min_v[i]</span>值决定，因而只要维护<span lang=EN-US>min_v[i]</span>值就够了。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">当前数必然可以插入到某个长度为<span lang=EN-US>j</span>（<span lang=EN-US>0&lt;=j&lt;=len</span>，<span lang=EN-US> len</span>为已找到的最长递增子序列长度）的序列后，使得旧的<span lang=EN-US>min_v[j+1]</span>（假设该数组每个值初始化为无穷大）大等于该数，必须更新<span lang=EN-US>min_v[j+1]</span>等于该数。如果序列是严格递增的，则只能找到一个这样的<span lang=EN-US>j</span>值；如果序列是非严格递增的，可能找到不止一个<span lang=EN-US>j</span>，取所有<span lang=EN-US>j</span>值最大的即可，因为插入到其它的，<span lang=EN-US>min_v[j+1]</span>值不会发生改变。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">具体过程：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">用<span lang=EN-US>min_v[i]</span>保存当前已找到的所有长度为<span lang=EN-US>i</span>的递增子序列的最后一个数的最小值。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">用<span lang=EN-US>len</span>保存当前已找到的最长递增子序列的长度。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">对输入<span lang=EN-US>src[n]</span>， 显然<span lang=EN-US> min_v[0]=src[0]</span>，这时<span lang=EN-US>len=1<o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">从输入数组的第二个数开始，<span lang=EN-US>(</span>假设要求所求子序列严格递增<span lang=EN-US>)<o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">比较当前值<span lang=EN-US>value</span>和长度为<span lang=EN-US>len</span>的子序列最后一个值<span lang=EN-US>(min_v[len-1])<o:p></o:p></span></span></p>
<p style="TEXT-INDENT: -10.5pt; MARGIN: 0cm 0cm 0pt 32.25pt; mso-char-indent-count: -1.0; mso-para-margin-left: 2.07gd" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>1</span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">若<span lang=EN-US>value &gt; min_v[len-1]</span>，则可以将<span lang=EN-US>value</span>加在已找到的最长递增子序列后面<span lang=EN-US>,</span>得到长度为<span lang=EN-US>len+1</span>的更长的递增子序列，即有<span lang=EN-US>min_v[len]=value</span>，且<span lang=EN-US>len</span>值加<span lang=EN-US>1</span>。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: -10.5pt; MARGIN: 0cm 0cm 0pt 31.5pt; mso-char-indent-count: -1.0; mso-para-margin-left: 2.0gd" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>2</span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">若<span lang=EN-US>value &lt;= min_v[len-1]</span>，则可以找到<span lang=EN-US>0&lt;=j&lt;=len-1</span>满足： <span lang=EN-US>value &lt;= min_v[j]</span>，<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><span style="mso-spacerun: yes">&nbsp;&nbsp; </span></span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">如果<span lang=EN-US>j=0</span>，则<span lang=EN-US>value</span>可组成长度为<span lang=EN-US>1</span>的子序列，<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 0.75pt; MARGIN: 0cm 0cm 0pt 36.75pt; mso-char-indent-count: .07; mso-para-margin-left: 3.5gd" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">如果<span lang=EN-US>j&gt;0</span>，则可以将<span lang=EN-US>value</span>加在由<span lang=EN-US>min_v[j-1]</span>决定的长度为<span lang=EN-US>j</span>的子序列后，得到新的长度为<span lang=EN-US>j+1</span>的子序列。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">均有<span lang=EN-US>min_v[j]=value<o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">如果要输出一个最长递增子序列，可以用一个数组<span lang=EN-US>info[i]</span>记录第<span lang=EN-US>i+1</span>个<span lang=EN-US>src[n]</span>数能得到最长递增子序列的长度（<span lang=EN-US>info[i]</span>等于前面要写入<span lang=EN-US>value</span>的位置在<span lang=EN-US>min_v</span>的下标加<span lang=EN-US>1</span>）。再同时扫描一遍这两个数组，从后往前扫描，先找到第一个对应长度为<span lang=EN-US>len</span>的数<span lang=EN-US>A</span>，然后找到第一个对应长度为<span lang=EN-US>len-1</span>且比<span lang=EN-US>A</span>小的数，以此类推找出所有的数。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">size_t&nbsp;lis(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;src[],&nbsp;size_t&nbsp;sz)<br><img id=Codehighlighter1_40_414_Open_Image onclick="this.style.display='none'; Codehighlighter1_40_414_Open_Text.style.display='none'; Codehighlighter1_40_414_Closed_Image.style.display='inline'; Codehighlighter1_40_414_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_40_414_Closed_Image onclick="this.style.display='none'; Codehighlighter1_40_414_Closed_Text.style.display='none'; Codehighlighter1_40_414_Open_Image.style.display='inline'; Codehighlighter1_40_414_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_40_414_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_40_414_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(src</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">NULL&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;sz</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">min_v[i]为当前已找到的长度为i+1的所有递增子序列的最后一个值的最小值。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;min_v(sz);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;min_v[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">src[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">len为当前已找到的最长递增子序列的长度</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;size_t&nbsp;i,len</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;value;<br><img id=Codehighlighter1_246_398_Open_Image onclick="this.style.display='none'; Codehighlighter1_246_398_Open_Text.style.display='none'; Codehighlighter1_246_398_Closed_Image.style.display='inline'; Codehighlighter1_246_398_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_246_398_Closed_Image onclick="this.style.display='none'; Codehighlighter1_246_398_Closed_Text.style.display='none'; Codehighlighter1_246_398_Open_Image.style.display='inline'; Codehighlighter1_246_398_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sz;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_246_398_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_246_398_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;value</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">src[i];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">假设递增子序列是严格递增&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">min_v[len</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])&nbsp;min_v[len</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">value;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">lower_bound(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">min_v[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">],</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">min_v[len],&nbsp;value)</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">value;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;len;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123549.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:39 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123549.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记16： 3.10 分层遍历二叉树</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123548.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:38:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123548.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123548.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123548.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123548.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123548.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>16</span>： <span lang=EN-US>3.10 </span>分层遍历二叉树<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">看到<span lang=EN-US>Milo</span>写的这篇<span lang=EN-US><a href="http://www.cnblogs.com/miloyip/archive/2010/05/12/binary_tree_traversal.html"><span lang=EN-US><span lang=EN-US><u><font color=#0000ff>文章</font></u></span></span></a></span>，又翻了下书，发现书的代码（<span lang=EN-US>P253</span>）有个瑕疵，每个节点值后面都会显示一个空格，如果将间隔字符改为&#8220;<span lang=EN-US>-</span>&#8221;，输出的每行最后都有一个&#8220;<span lang=EN-US>-</span>&#8221;，不能达到要求。不过，只要将 <span lang=EN-US>cout &lt;&lt; vec[cur] -&gt; data &lt;&lt; " "; <o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">这行改为：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>if (cur==last-1) cout &lt;&lt; vec[cur] -&gt; data &lt;&lt; "\n";<o:p></o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>else cout &lt;&lt; vec[cur] -&gt; data &lt;&lt; " ";<o:p></o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">即可修正这个问题。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">书上的代码用了两个<span lang=EN-US>while</span>循环，可以精简为一个。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">思路：保存每层的最后一个节点位置（取节点的地址或在容器内的位置），当遍历到该位置时，获取下一层最后一个节点的位置，如果这两个位置相同，说明已经遍历完全部节点，否则开始下一层的遍历。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">由于不知道树的节点数，很多情况下，容器采用<span lang=EN-US>deque</span>比采用<span lang=EN-US>vector</span>性能更佳，因为避免了申请内存后对原数据的拷贝。</span><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white">另外，再考虑到<span lang=EN-US>deque</span>的数组下标访问要比采用迭代器访问慢很多</span><span style="FONT-FAMILY: 宋体; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">，最好采用迭代器来访问内部数据。</span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p></o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;<br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_003648 onclick="this.style.display='none'; Code_Closed_Text_003648.style.display='none'; Code_Open_Image_003648.style.display='inline'; Code_Open_Text_003648.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_003648 onclick="this.style.display='none'; Code_Open_Text_003648.style.display='none'; Code_Closed_Image_003648.style.display='inline'; Code_Closed_Text_003648.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_003648>代码一</span><span style="DISPLAY: none" id=Code_Open_Text_003648><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img id=Codehighlighter1_13_56_Open_Image onclick="this.style.display='none'; Codehighlighter1_13_56_Open_Text.style.display='none'; Codehighlighter1_13_56_Closed_Image.style.display='inline'; Codehighlighter1_13_56_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_13_56_Closed_Image onclick="this.style.display='none'; Codehighlighter1_13_56_Closed_Text.style.display='none'; Codehighlighter1_13_56_Open_Image.style.display='inline'; Codehighlighter1_13_56_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;Node&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_13_56_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_13_56_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;Node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">left;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;Node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">right;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;data;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_93_559_Open_Image onclick="this.style.display='none'; Codehighlighter1_93_559_Open_Text.style.display='none'; Codehighlighter1_93_559_Closed_Image.style.display='inline'; Codehighlighter1_93_559_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_93_559_Closed_Image onclick="this.style.display='none'; Codehighlighter1_93_559_Closed_Text.style.display='none'; Codehighlighter1_93_559_Open_Image.style.display='inline'; Codehighlighter1_93_559_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;print_tree_bfs_d(Node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;root)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_93_559_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_93_559_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(root</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">NULL)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;deque</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Node</span><span style="COLOR: #000000">*&gt;</span><span style="COLOR: #000000">&nbsp;tree;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;tree.push_back(root);<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;deque</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Node</span><span style="COLOR: #000000">*&gt;</span><span style="COLOR: #000000">::iterator&nbsp;low</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tree.begin(),last</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tree.begin();<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;Node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">node</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">root;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_257_541_Open_Image onclick="this.style.display='none'; Codehighlighter1_257_541_Open_Text.style.display='none'; Codehighlighter1_257_541_Closed_Image.style.display='inline'; Codehighlighter1_257_541_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_257_541_Closed_Image onclick="this.style.display='none'; Codehighlighter1_257_541_Closed_Text.style.display='none'; Codehighlighter1_257_541_Open_Image.style.display='inline'; Codehighlighter1_257_541_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_257_541_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_257_541_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">left)&nbsp;&nbsp;tree.push_back(node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">left);<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">right)&nbsp;tree.push_back(node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">right);<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img id=Codehighlighter1_374_413_Open_Image onclick="this.style.display='none'; Codehighlighter1_374_413_Open_Text.style.display='none'; Codehighlighter1_374_413_Closed_Image.style.display='inline'; Codehighlighter1_374_413_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_374_413_Closed_Image onclick="this.style.display='none'; Codehighlighter1_374_413_Closed_Text.style.display='none'; Codehighlighter1_374_413_Open_Image.style.display='inline'; Codehighlighter1_374_413_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(low</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">last)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_374_413_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_374_413_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img id=Codehighlighter1_418_520_Open_Image onclick="this.style.display='none'; Codehighlighter1_418_520_Open_Text.style.display='none'; Codehighlighter1_418_520_Closed_Image.style.display='inline'; Codehighlighter1_418_520_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_418_520_Closed_Image onclick="this.style.display='none'; Codehighlighter1_418_520_Closed_Text.style.display='none'; Codehighlighter1_418_520_Open_Image.style.display='inline'; Codehighlighter1_418_520_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #0000ff">else</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_418_520_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_418_520_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(last</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">tree.end()</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tree.end()</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;node</span><span style="COLOR: #000000">=*++</span><span style="COLOR: #000000">low;<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></span></div>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><br><br></o:p></span><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white">上面的代码，保留了树的所有全部节点，稍做修改（比如用一个数组记录每层的最后一个节点的位置），可以查询某层的所有节点。如果不需要保存中间结果，可以修改为：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white" lang=EN-US><o:p>&nbsp;<br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_003735 onclick="this.style.display='none'; Code_Closed_Text_003735.style.display='none'; Code_Open_Image_003735.style.display='inline'; Code_Open_Text_003735.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_003735 onclick="this.style.display='none'; Code_Open_Text_003735.style.display='none'; Code_Closed_Image_003735.style.display='inline'; Code_Closed_Text_003735.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_003735>代码二</span><span style="DISPLAY: none" id=Code_Open_Text_003735><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img id=Codehighlighter1_36_475_Open_Image onclick="this.style.display='none'; Codehighlighter1_36_475_Open_Text.style.display='none'; Codehighlighter1_36_475_Closed_Image.style.display='inline'; Codehighlighter1_36_475_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_36_475_Closed_Image onclick="this.style.display='none'; Codehighlighter1_36_475_Closed_Text.style.display='none'; Codehighlighter1_36_475_Open_Image.style.display='inline'; Codehighlighter1_36_475_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;print_tree_bfs_dq(Node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;root)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_36_475_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_36_475_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(root</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">NULL)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;deque</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">Node</span><span style="COLOR: #000000">*&gt;</span><span style="COLOR: #000000">&nbsp;tree;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;tree.push_back(root);<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;Node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">node</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">root,&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">last</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">root;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_151_457_Open_Image onclick="this.style.display='none'; Codehighlighter1_151_457_Open_Text.style.display='none'; Codehighlighter1_151_457_Closed_Image.style.display='inline'; Codehighlighter1_151_457_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_151_457_Closed_Image onclick="this.style.display='none'; Codehighlighter1_151_457_Closed_Text.style.display='none'; Codehighlighter1_151_457_Open_Image.style.display='inline'; Codehighlighter1_151_457_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_151_457_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_151_457_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;node</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tree.front();<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;tree.pop_front();<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">left)&nbsp;&nbsp;tree.push_back(node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">left);<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">right)&nbsp;tree.push_back(node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">right);<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_314_353_Open_Image onclick="this.style.display='none'; Codehighlighter1_314_353_Open_Text.style.display='none'; Codehighlighter1_314_353_Closed_Image.style.display='inline'; Codehighlighter1_314_353_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_314_353_Closed_Image onclick="this.style.display='none'; Codehighlighter1_314_353_Closed_Text.style.display='none'; Codehighlighter1_314_353_Open_Image.style.display='inline'; Codehighlighter1_314_353_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(node</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">last)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_314_353_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_314_353_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img id=Codehighlighter1_358_453_Open_Image onclick="this.style.display='none'; Codehighlighter1_358_453_Open_Text.style.display='none'; Codehighlighter1_358_453_Closed_Image.style.display='inline'; Codehighlighter1_358_453_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_358_453_Closed_Image onclick="this.style.display='none'; Codehighlighter1_358_453_Closed_Text.style.display='none'; Codehighlighter1_358_453_Open_Image.style.display='inline'; Codehighlighter1_358_453_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #0000ff">else</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_358_453_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_358_453_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;node</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(tree.empty())&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tree.back();<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></span></div>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><br><br></o:p></span><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white">当然也可以用<span lang=EN-US>queue</span>（<span lang=EN-US>queue</span>只是对<span lang=EN-US>deque</span>的封装）。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">对问题<span lang=EN-US>2</span>，上面的代码只要做稍微修改，只在遍历到所要求的层才输出，输出后直接返回就可以了。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p></o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123548.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:38 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123548.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记14： 4.4 是否在三角形内</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123546.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:34:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123546.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123546.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123546.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123546.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123546.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>14</span>： <span lang=EN-US>4.4 </span>是否在三角形内<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<span style="FONT-FAMILY: 宋体; FONT-SIZE: 10.5pt; mso-font-kerning: 1.0pt; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA" lang=EN-US><span style="mso-spacerun: yes">&nbsp; </span><span style="mso-spacerun: yes">&nbsp;&nbsp;</span></span><span style="FONT-FAMILY: 宋体; FONT-SIZE: 10.5pt; mso-font-kerning: 1.0pt; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">对几何题目，常用到矢量。若<span lang=EN-US>P</span>点在三角形内部，则矢量<span lang=EN-US>PA</span>沿时钟某个方向三次旋转分别经过<span lang=EN-US>PB</span>、<span lang=EN-US>PC</span>再回到<span lang=EN-US>PA</span>，每次旋转角度都不会超过<span lang=EN-US>180</span>度。即PA&#215;PB、PB&#215;PC和PC&#215;PA这三个矢量积应该都为正，或都为负。如果<span lang=EN-US>P</span>在三角形边上，则这三个矢量积应该一个为<span lang=EN-US>0</span>，其它两个同正或同负。若在三角形某个顶点，则三个矢量积，必有两个为<span lang=EN-US>0</span>，一个不为<span lang=EN-US>0</span>。若<span lang=EN-US>A B C</span>三点共线，<span lang=EN-US>P</span>在这直线上，则所有矢量积均为<span lang=EN-US>0</span>，若<span lang=EN-US>P</span>不在这直线上，则为两正一负或两负一正。<br><br><br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img id=Codehighlighter1_13_39_Open_Image onclick="this.style.display='none'; Codehighlighter1_13_39_Open_Text.style.display='none'; Codehighlighter1_13_39_Closed_Image.style.display='inline'; Codehighlighter1_13_39_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_13_39_Closed_Image onclick="this.style.display='none'; Codehighlighter1_13_39_Closed_Text.style.display='none'; Codehighlighter1_13_39_Open_Image.style.display='inline'; Codehighlighter1_13_39_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;Point</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_13_39_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_13_39_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;x;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;y;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;direction(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Point</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;a,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Point</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;b,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Point</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;p)<br><img id=Codehighlighter1_112_265_Open_Image onclick="this.style.display='none'; Codehighlighter1_112_265_Open_Text.style.display='none'; Codehighlighter1_112_265_Closed_Image.style.display='inline'; Codehighlighter1_112_265_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_112_265_Closed_Image onclick="this.style.display='none'; Codehighlighter1_112_265_Closed_Text.style.display='none'; Codehighlighter1_112_265_Open_Image.style.display='inline'; Codehighlighter1_112_265_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_112_265_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_112_265_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;zero</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1e</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(p.x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">a.x)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(p.y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">b.y)</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(p.x</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">b.x)</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(p.y</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">a.y);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(tmp&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;zero)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(tmp&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">zero)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;is_in_triangle(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Point</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;a,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Point</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;b,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Point</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;c,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Point</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;p)<br><img id=Codehighlighter1_352_826_Open_Image onclick="this.style.display='none'; Codehighlighter1_352_826_Open_Text.style.display='none'; Codehighlighter1_352_826_Closed_Image.style.display='inline'; Codehighlighter1_352_826_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_352_826_Closed_Image onclick="this.style.display='none'; Codehighlighter1_352_826_Closed_Text.style.display='none'; Codehighlighter1_352_826_Open_Image.style.display='inline'; Codehighlighter1_352_826_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_352_826_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_352_826_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;direction(a,b,p)</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;direction(b,c,p)&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;direction(c,a,p);<br><img id=Codehighlighter1_420_570_Open_Image onclick="this.style.display='none'; Codehighlighter1_420_570_Open_Text.style.display='none'; Codehighlighter1_420_570_Closed_Image.style.display='inline'; Codehighlighter1_420_570_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_420_570_Closed_Image onclick="this.style.display='none'; Codehighlighter1_420_570_Closed_Text.style.display='none'; Codehighlighter1_420_570_Open_Image.style.display='inline'; Codehighlighter1_420_570_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_420_570_Closed_Text>/**/</span><span id=Codehighlighter1_420_570_Open_Text><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;if&nbsp;(t==3&nbsp;||&nbsp;t==12)&nbsp;return&nbsp;true;&nbsp;&nbsp;&nbsp;//在三角形ABC内<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;if&nbsp;(t==1&nbsp;||&nbsp;t==4)&nbsp;&nbsp;return&nbsp;true;&nbsp;&nbsp;//与点A&nbsp;B&nbsp;C之一重合<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;if&nbsp;(t==2&nbsp;||&nbsp;t==8)&nbsp;&nbsp;return&nbsp;true;&nbsp;&nbsp;//在三角形ABC边上。<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(t</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;t</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(t</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;t</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;&nbsp;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">在三角形内a[3]=a[12]=1&nbsp;在边上&nbsp;a[2]=a[8]=1&nbsp;在顶点a[1]=a[4]=1;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">int&nbsp;a[]={0,&nbsp;1,1,1,1,&nbsp;0,0,0,&nbsp;1,&nbsp;0,0,0,&nbsp;1};<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">return&nbsp;a[direction(a,b,p)+direction(b,c,p)+direction(c,a,p)];&nbsp;&nbsp;</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="COLOR: #000000">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
</span>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123546.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:34 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123546.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记15： 4.5 磁带文件存放优化</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123547.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:34:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123547.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123547.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123547.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123547.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123547.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>15</span>： <span lang=EN-US>4.5 </span>磁带文件存放优化<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">对一个已经是最优解的排列，记第<span lang=EN-US>i</span>个文件的长度为<span lang=EN-US>B<sub>i</sub></span>，被访问概率为<span lang=EN-US>A<sub>i</sub></span>。如果交换第<span lang=EN-US>i</span>个和第<span lang=EN-US>i+1</span>个文件，则平均长度一定不会变小，交换后，访问原第<span lang=EN-US>i</span>个文件时，要多访问一个原第<span lang=EN-US>i+1</span>文件，长度增加了<span lang=EN-US>A<sub>i</sub>*B<sub>i+1</sub></span>，而访问原第<span lang=EN-US>i+1</span>个文件时，要少访问一个原第<span lang=EN-US>i</span>个文件，长度减少了<span lang=EN-US>A<sub>i+1</sub>*B<sub>i</sub></span>，而访问这两个文件之前和之后的文件，长度没有改变，故有：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>A<sub>i</sub>*B<sub>i+1 </sub>- A<sub>i+1</sub>*B<sub>i </sub>&gt;=0 </span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">即<span lang=EN-US>A<sub>i</sub>/B<sub>i</sub> &gt;= A<sub>i+1</sub>/B<sub>i+1</sub></span>，<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">由于<span lang=EN-US>i</span>可以任意取，因而<span lang=EN-US>A<sub>0</sub>/B<sub>0</sub> &gt;= A<sub>1</sub>/B<sub>1</sub> &gt;= A<sub>2</sub>/B<sub>2</sub> &gt;= &#8230; &gt;= A<sub>n-1</sub>/B<sub>n-1</sub></span>。也就是说，最优解一定满足<span lang=EN-US>P[i]/L[i] </span>按降序排列，而根据<span lang=EN-US>P[i]/L[i]</span>值通过降序排列只能得到唯一确定的排列（只考虑<span lang=EN-US>P[i]/L[i]</span>值大小，如果两个文件的<span lang=EN-US>P[i]/L[i]</span>值相同，它们位置可以互换而不影响），这个排列必然是最优解。因而，原问题等同于&#8220;<span style="COLOR: blue">将<span lang=EN-US>P[i]/L[i] </span>降序排列</span>&#8221;。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">类似题：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 10.5pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 1.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">&#8220;将一个给定的自然数数组，连接起来得到一个数，求这个数的最大值或最小值&#8221;。<span lang=EN-US><o:p></o:p></span></span></p>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123547.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:34 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123547.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记13： 4.1 金刚坐飞机问题</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123545.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:29:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123545.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123545.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123545.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123545.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123545.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>13</span>： </span><span style="FONT-FAMILY: 宋体" lang=EN-US>4.1 </span><span style="FONT-FAMILY: 宋体">金刚坐飞机问题<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体">问题：<span lang=EN-US><o:p></o:p></span></span></strong></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体" lang=EN-US><span style="mso-spacerun: yes">&nbsp; </span><span style="mso-spacerun: yes">&nbsp;&nbsp;</span></span></strong><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体">现在有一班飞机将要起飞，乘客们正准备按机票号码（<span lang=EN-US>1, 2, 3, </span>&#8230;<span lang=EN-US>N</span>）依次排队登机。突然来了一只大猩猩（对，他叫金刚）。他也有飞机票，但是他插队第一个登上了飞机，然后随意地选了一个座位坐下了<span lang=EN-US>1</span>。根据社会的和谐程度，其他的乘客有两种反应：<span lang=EN-US> <o:p></o:p></span></span></strong></p>
<p style="TEXT-INDENT: 21.1pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体" lang=EN-US>1. </span></strong><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体">乘客们都义愤填膺，&#8220;既然金刚同志不遵守规定，为什么我要遵守？&#8221;他们也随意地找位置坐下，并且坚决不让座给其他乘客。<span lang=EN-US> <o:p></o:p></span></span></strong></p>
<p style="TEXT-INDENT: 21.1pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体" lang=EN-US>2. </span></strong><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体">乘客们虽然感到愤怒，但还是以&#8220;和谐&#8221;为重，如果自己的位置没有被占领，就赶紧坐下，如果自己的位置已经被别人（或者金刚同志）占了，就随机地选择另一个位置坐下，并开始闭目养神，不再挪动位置。<span lang=EN-US> <o:p></o:p></span></span></strong></p>
<p style="TEXT-INDENT: 21.1pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体">那么，在这两种情况下，第<span lang=EN-US> i </span>个乘客（除去金刚同志之外）坐到自己原机票位置的概率分别是多少？<span lang=EN-US><o:p></o:p></span></span></strong></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体">对问题一，每个人都是随机选择座位，任意一个人坐在指定座位的概率相同，因而第<span lang=EN-US>i</span>个乘客坐在其座位的概率是<span lang=EN-US> 1/n</span>。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体">对问题二，答案和金刚的原来座位编号有关。不妨先去除金刚的座位，将乘客（根据机票号）和剩下的座位，按原大小顺序从<span lang=EN-US>1</span>开始重新编号。<em style="mso-bidi-font-style: normal">用<span lang=EN-US>F(i</span>，<span lang=EN-US>n)</span>表示在新排列中（共有<span lang=EN-US>n-1</span>个乘客座位和金刚原来的座位），新的第<span lang=EN-US>i</span>个乘客坐在其原来座位的概率，</em>则在<span lang=EN-US>n</span>个座位中：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体">① 金刚若挑自己的座位或选的座位在第<span lang=EN-US>i</span>个座位后（共有<span lang=EN-US>n-i</span>个座位满足这个条件），则第<span lang=EN-US>i</span>个乘客肯定能坐到原来的座位；<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体">② 金刚若挑选的座位在第<span lang=EN-US>i</span>个座位前，不妨假设为<span lang=EN-US>j</span>，则第<span lang=EN-US>j</span>个乘客除非坐到金刚的座位，不然就会抢其他人的座位，因为他的行为和金刚相似，可以将他当做金刚处理。去除前<span lang=EN-US>j</span>个座位，剩下的座位和乘客再按原大小排序重新从<span lang=EN-US>1</span>开始编号，则先前的第<span lang=EN-US>i</span>个乘客，其座位号变为<span lang=EN-US>i-j</span>，新的总座位数变为<span lang=EN-US>n-j</span>。因而可得公式：<br><br><span lang=EN-US><o:p></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体">用<span lang=EN-US> G(i, n)</span>表示原排列中，第<span lang=EN-US>i</span>个乘客坐到自己座位的概率，假设金刚的座位编号为<span lang=EN-US>j</span>。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体">若<span lang=EN-US> i&lt;j </span>则 <span lang=EN-US><span style="mso-spacerun: yes">&nbsp;&nbsp;</span>G(i,n)=F(i,n)= (n-i)/(n+1-i)</span>。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体">若<span lang=EN-US> i&gt;j </span>则<span lang=EN-US><span style="mso-spacerun: yes">&nbsp;&nbsp; </span>G(i,n)=F(i-1,n)= (n+1-i)/(n+2-i) </span>。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体" lang=EN-US><o:p>&nbsp;<img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/flyinghearts/4-1.jpg" width=709 height=221><br><br></o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体">类似题：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体">&#8220;约瑟芬环：<span lang=EN-US>n</span>个人，编号为<span lang=EN-US>0</span>到<span lang=EN-US>n-1</span>，围成一圈，从编号为<span lang=EN-US>0</span>的开始，从<span lang=EN-US>1</span>开始报数，所有报到<span lang=EN-US>m</span>的出列，下一个人从<span lang=EN-US>1</span>开始继续报数。求最后一个人的编号。&#8221;<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体" lang=EN-US><o:p></o:p></span></o:p></span></span></p>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123545.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:29 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123545.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记19： 3.9 重建二叉树</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123544.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:27:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123544.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123544.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123544.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123544.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123544.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>19</span>： <span lang=EN-US>3.9 </span>重建二叉树<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">对根节点<span lang=EN-US>a</span>以及先序遍历次序<span lang=EN-US>P</span>和中序遍历次序<span lang=EN-US>I</span>，查找<span lang=EN-US>a</span>在<span lang=EN-US>I</span>中的位置，将<span lang=EN-US>I</span>分为两部分，左边部分的元素都在<span lang=EN-US>a</span>的左子树上，右边的元素都在<span lang=EN-US>a</span>的右子树上，因而可以确定<span lang=EN-US>a</span>的左子树节点数和<span lang=EN-US>a</span>的右子树节点数，再结合<span lang=EN-US>P</span>，可以确定<span lang=EN-US>a</span>的左孩子和右孩子，以及各个孩子的先序和中序遍历次序。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">由于已经知道节点数，可以事先分配好内存，可以按先序遍历次序连续存放节点。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;<br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_002352 onclick="this.style.display='none'; Code_Closed_Text_002352.style.display='none'; Code_Open_Image_002352.style.display='inline'; Code_Open_Text_002352.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_002352 onclick="this.style.display='none'; Code_Open_Text_002352.style.display='none'; Code_Closed_Image_002352.style.display='inline'; Code_Closed_Text_002352.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_002352>rebuild_tree_1</span><span style="DISPLAY: none" id=Code_Open_Text_002352><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img id=Codehighlighter1_13_57_Open_Image onclick="this.style.display='none'; Codehighlighter1_13_57_Open_Text.style.display='none'; Codehighlighter1_13_57_Closed_Image.style.display='inline'; Codehighlighter1_13_57_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_13_57_Closed_Image onclick="this.style.display='none'; Codehighlighter1_13_57_Closed_Text.style.display='none'; Codehighlighter1_13_57_Open_Image.style.display='inline'; Codehighlighter1_13_57_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;Node&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_13_57_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_13_57_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;Node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;left;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;Node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;right;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;data;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;rebuild(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;preorder[],&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;inorder[],&nbsp;Node&nbsp;result[],&nbsp;size_t&nbsp;size)<br><img id=Codehighlighter1_135_642_Open_Image onclick="this.style.display='none'; Codehighlighter1_135_642_Open_Text.style.display='none'; Codehighlighter1_135_642_Closed_Image.style.display='inline'; Codehighlighter1_135_642_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_135_642_Closed_Image onclick="this.style.display='none'; Codehighlighter1_135_642_Closed_Text.style.display='none'; Codehighlighter1_135_642_Open_Image.style.display='inline'; Codehighlighter1_135_642_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_135_642_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_135_642_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">preorder;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">left</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">right</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;inorder;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;size_t&nbsp;left_size</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(left_size</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">size&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">++!=*</span><span style="COLOR: #000000">preorder)&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">left_size;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;assert&nbsp;(left_size</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">size);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;size_t&nbsp;right_size&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;size</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">left_size;<br><img id=Codehighlighter1_392_478_Open_Image onclick="this.style.display='none'; Codehighlighter1_392_478_Open_Text.style.display='none'; Codehighlighter1_392_478_Closed_Image.style.display='inline'; Codehighlighter1_392_478_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_392_478_Closed_Image onclick="this.style.display='none'; Codehighlighter1_392_478_Closed_Text.style.display='none'; Codehighlighter1_392_478_Open_Image.style.display='inline'; Codehighlighter1_392_478_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(left_size)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_392_478_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_392_478_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">left</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">result</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;rebuild(preorder</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;inorder,&nbsp;result</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;left_size);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img id=Codehighlighter1_498_640_Open_Image onclick="this.style.display='none'; Codehighlighter1_498_640_Open_Text.style.display='none'; Codehighlighter1_498_640_Closed_Image.style.display='inline'; Codehighlighter1_498_640_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_498_640_Closed_Image onclick="this.style.display='none'; Codehighlighter1_498_640_Closed_Text.style.display='none'; Codehighlighter1_498_640_Open_Image.style.display='inline'; Codehighlighter1_498_640_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(right_size)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_498_640_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_498_640_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">right</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">result</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;rebuild(preorder</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;inorder</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;right_size);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></span></div>
</o:p></span><br>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none; mso-char-indent-count: 2.0" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white">上面的代码，栈深度是<span lang=EN-US>O(n)</span>，有可能出现栈溢出，可以修改代码，减少一次递归调用，实现栈深度为<span lang=EN-US>O(lg n)</span>。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white" lang=EN-US><span style="mso-spacerun: yes">&nbsp;</span><o:p></o:p></span></p>
<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_002444 onclick="this.style.display='none'; Code_Closed_Text_002444.style.display='none'; Code_Open_Image_002444.style.display='inline'; Code_Open_Text_002444.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_002444 onclick="this.style.display='none'; Code_Open_Text_002444.style.display='none'; Code_Closed_Image_002444.style.display='inline'; Code_Closed_Text_002444.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_002444>rebuild_tree_2</span><span style="DISPLAY: none" id=Code_Open_Text_002444><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;rebuild2(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;preorder[],&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;inorder[],&nbsp;Node&nbsp;result[],&nbsp;size_t&nbsp;size)<br><img id=Codehighlighter1_76_1033_Open_Image onclick="this.style.display='none'; Codehighlighter1_76_1033_Open_Text.style.display='none'; Codehighlighter1_76_1033_Closed_Image.style.display='inline'; Codehighlighter1_76_1033_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_76_1033_Closed_Image onclick="this.style.display='none'; Codehighlighter1_76_1033_Closed_Text.style.display='none'; Codehighlighter1_76_1033_Open_Image.style.display='inline'; Codehighlighter1_76_1033_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_76_1033_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_76_1033_Open_Text><span style="COLOR: #000000">{<br><img id=Codehighlighter1_89_1031_Open_Image onclick="this.style.display='none'; Codehighlighter1_89_1031_Open_Text.style.display='none'; Codehighlighter1_89_1031_Closed_Image.style.display='inline'; Codehighlighter1_89_1031_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_89_1031_Closed_Image onclick="this.style.display='none'; Codehighlighter1_89_1031_Closed_Text.style.display='none'; Codehighlighter1_89_1031_Open_Image.style.display='inline'; Codehighlighter1_89_1031_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_89_1031_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_89_1031_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">preorder;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">left</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">right</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;inorder;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;left_size</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(left_size</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">size&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">++!=*</span><span style="COLOR: #000000">preorder)&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">left_size;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;assert&nbsp;(left_size</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">size);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;right_size&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;size</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">left_size;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(left_size</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;right_size</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br><img id=Codehighlighter1_421_704_Open_Image onclick="this.style.display='none'; Codehighlighter1_421_704_Open_Text.style.display='none'; Codehighlighter1_421_704_Closed_Image.style.display='inline'; Codehighlighter1_421_704_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_421_704_Closed_Image onclick="this.style.display='none'; Codehighlighter1_421_704_Closed_Text.style.display='none'; Codehighlighter1_421_704_Open_Image.style.display='inline'; Codehighlighter1_421_704_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(left_size</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">right_size)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_421_704_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_421_704_Open_Text><span style="COLOR: #000000">{<br><img id=Codehighlighter1_444_543_Open_Image onclick="this.style.display='none'; Codehighlighter1_444_543_Open_Text.style.display='none'; Codehighlighter1_444_543_Closed_Image.style.display='inline'; Codehighlighter1_444_543_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_444_543_Closed_Image onclick="this.style.display='none'; Codehighlighter1_444_543_Closed_Text.style.display='none'; Codehighlighter1_444_543_Open_Image.style.display='inline'; Codehighlighter1_444_543_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(left_size)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_444_543_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_444_543_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">left</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">result</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rebuild2(preorder</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;inorder,&nbsp;result</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;left_size);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">right</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">result</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;preorder&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inorder&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;right_size;<br><img id=Codehighlighter1_711_1027_Open_Image onclick="this.style.display='none'; Codehighlighter1_711_1027_Open_Text.style.display='none'; Codehighlighter1_711_1027_Closed_Image.style.display='inline'; Codehighlighter1_711_1027_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_711_1027_Closed_Image onclick="this.style.display='none'; Codehighlighter1_711_1027_Closed_Text.style.display='none'; Codehighlighter1_711_1027_Open_Image.style.display='inline'; Codehighlighter1_711_1027_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_711_1027_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_711_1027_Open_Text><span style="COLOR: #000000">{<br><img id=Codehighlighter1_735_895_Open_Image onclick="this.style.display='none'; Codehighlighter1_735_895_Open_Text.style.display='none'; Codehighlighter1_735_895_Closed_Image.style.display='inline'; Codehighlighter1_735_895_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_735_895_Closed_Image onclick="this.style.display='none'; Codehighlighter1_735_895_Closed_Text.style.display='none'; Codehighlighter1_735_895_Open_Image.style.display='inline'; Codehighlighter1_735_895_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(right_size)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_735_895_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_735_895_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">right</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">result</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rebuild2(preorder</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;inorder</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">left_size</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;right_size);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img id=Codehighlighter1_918_1021_Open_Image onclick="this.style.display='none'; Codehighlighter1_918_1021_Open_Text.style.display='none'; Codehighlighter1_918_1021_Closed_Image.style.display='inline'; Codehighlighter1_918_1021_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_918_1021_Closed_Image onclick="this.style.display='none'; Codehighlighter1_918_1021_Closed_Text.style.display='none'; Codehighlighter1_918_1021_Open_Image.style.display='inline'; Codehighlighter1_918_1021_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(left_size)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_918_1021_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_918_1021_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">left</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">result</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">preorder;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">result;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;left_size;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></span></div>
<br>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white">书上的代码（<span lang=EN-US>P246</span>）：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><strong><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: navy; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white" lang=EN-US></span></strong><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white" lang=EN-US><o:p>&nbsp;</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_002646 onclick="this.style.display='none'; Code_Closed_Text_002646.style.display='none'; Code_Open_Image_002646.style.display='inline'; Code_Open_Text_002646.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_002646 onclick="this.style.display='none'; Code_Open_Text_002646.style.display='none'; Code_Closed_Image_002646.style.display='inline'; Code_Closed_Text_002646.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_002646></span><span style="DISPLAY: none" id=Code_Open_Text_002646><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">&nbsp;&nbsp;NODE</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;pTemp&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;NODE;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;pTemp&nbsp;</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">&nbsp;chValue&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">pPreOrder;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;pTemp&nbsp;</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">&nbsp;pLeft&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;NULL;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;pTemp&nbsp;</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">&nbsp;pRight&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;NULL;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;如果节点为空，把当前节点复制到根节点</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">pRoot&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;NULL)<br><img id=Codehighlighter1_160_187_Open_Image onclick="this.style.display='none'; Codehighlighter1_160_187_Open_Text.style.display='none'; Codehighlighter1_160_187_Closed_Image.style.display='inline'; Codehighlighter1_160_187_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_160_187_Closed_Image onclick="this.style.display='none'; Codehighlighter1_160_187_Closed_Text.style.display='none'; Codehighlighter1_160_187_Open_Image.style.display='inline'; Codehighlighter1_160_187_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_160_187_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_160_187_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">pRoot&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;pTemp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">&nbsp;&nbsp;}</span></span></span></div>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left></o:p></span>&nbsp;</p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white">可能引起内存泄漏（当</span><strong><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: navy; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white" lang=EN-US>*</span></strong><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white" lang=EN-US>pRoot!=NULL</span><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white">，新申请的内存没释放），注释也不对（不是复制节点，而是更改指针指向新建的节点）。另外，频繁的<span lang=EN-US>new</span>，极有可能会产生内存碎片。当节点很小时，内存浪费很严重（每<span lang=EN-US>new</span>一次都要额外分配空间储存相关信息）。<span lang=EN-US><o:p></o:p></span></span></p>
<br>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123544.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:27 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123544.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记12： 3.8 求二叉树中节点的最大距离</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123543.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:22:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123543.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123543.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123543.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123543.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123543.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>12</span>： <span lang=EN-US>3.8 </span>求二叉树中节点的最大距离<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">问题：<span lang=EN-US><o:p></o:p></span></span></strong></p>
<p style="TEXT-INDENT: 21.1pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">如果我们把二叉树看成一个图，父子节点之间的连线看成是双向的，我们姑且定义<span lang=EN-US>"</span>距离<span lang=EN-US>"</span>为两节点之间边的个数。写一个程序求一棵二叉树中相距最远的两个节点之间的距离。<span lang=EN-US><o:p></o:p></span></span></strong></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><em style="mso-bidi-font-style: normal"><span style="FONT-FAMILY: 宋体; COLOR: blue; mso-bidi-font-size: 10.5pt">实际上就是求树的直径。</span></em><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">若采用&#8220;动态规划方法&#8221;思想，会将该问题分解成&#8220;具有最大距离两点间的路径是否经过根节点&#8221;两个子问题，然后再对这两个子问题求解判断。实际上，不必这么麻烦。<em style="mso-bidi-font-style: normal"><span style="COLOR: #3366ff">距离最远的两点必然在以某个节点<span lang=EN-US>A</span>为根的子树上，它们间的路径必然经过该子树的根节点<span lang=EN-US>A</span>。因而，以任意一个节点<span lang=EN-US>B</span>为根的子树，计算出经过该子树根节点<span lang=EN-US>B</span>的最大距离，则所有最大距离的最大值就是所要求的二叉树的最大距离，即&#8220;树的直径&#8221;。</span></em>而经过树的根节点的最大距离为：左子树的高度<span lang=EN-US>+</span>右子树的高度<span lang=EN-US>+2</span>（假设空节点的高度为<span lang=EN-US>-1</span>），因而，<strong style="mso-bidi-font-weight: normal"><span style="COLOR: blue">原问题等同于&#8220;计算每个节点的左子树和右子树的高度和，取最大值&#8221;</span></strong><span style="COLOR: blue">。</span><span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><br><br><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white; mso-hansi-font-family: 'Times New Roman'" lang=EN-US><o:p></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img id=Codehighlighter1_13_56_Open_Image onclick="this.style.display='none'; Codehighlighter1_13_56_Open_Text.style.display='none'; Codehighlighter1_13_56_Closed_Image.style.display='inline'; Codehighlighter1_13_56_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_13_56_Closed_Image onclick="this.style.display='none'; Codehighlighter1_13_56_Closed_Text.style.display='none'; Codehighlighter1_13_56_Open_Image.style.display='inline'; Codehighlighter1_13_56_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;Node&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_13_56_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_13_56_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;Node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">left;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;Node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">right;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;data;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tree_height(Node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;root,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;max_distance)<br><img id=Codehighlighter1_114_495_Open_Image onclick="this.style.display='none'; Codehighlighter1_114_495_Open_Text.style.display='none'; Codehighlighter1_114_495_Closed_Image.style.display='inline'; Codehighlighter1_114_495_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_114_495_Closed_Image onclick="this.style.display='none'; Codehighlighter1_114_495_Closed_Text.style.display='none'; Codehighlighter1_114_495_Open_Image.style.display='inline'; Codehighlighter1_114_495_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_114_495_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_114_495_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">每碰到一个子节点，高度自增1，可以设空节点高度为-1，<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">避免计算高度时对空节点的判断。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(root&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;NULL)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;left_height&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;tree_height(root</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">left,&nbsp;max_distance)&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;right_height&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;tree_height(root</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">right,&nbsp;max_distance)&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;distance&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;left_height&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;right_height;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(max_distance&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;distance)&nbsp;max_distance&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;distance;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;&nbsp;left_height&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;right_height&nbsp;</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">&nbsp;left_height&nbsp;:&nbsp;right_height;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tree_diameter(Node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;root)<br><img id=Codehighlighter1_528_612_Open_Image onclick="this.style.display='none'; Codehighlighter1_528_612_Open_Text.style.display='none'; Codehighlighter1_528_612_Closed_Image.style.display='inline'; Codehighlighter1_528_612_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_528_612_Closed_Image onclick="this.style.display='none'; Codehighlighter1_528_612_Closed_Text.style.display='none'; Codehighlighter1_528_612_Open_Image.style.display='inline'; Codehighlighter1_528_612_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_528_612_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_528_612_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;max_distance&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;tree_height(root,&nbsp;max_distance);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;max_distance;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
</o:p></span>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123543.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:22 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123543.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记11： 3.3 计算字符串的相似度</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123542.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:21:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123542.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123542.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123542.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123542.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123542.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>11</span>： <span lang=EN-US>3.3 </span>计算字符串的相似度<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">很经典的可使用动态规划方法解决的题目，和计算两字符串的最长公共子序列相似。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">设<span lang=EN-US>A<sub>i</sub></span>为字符串<span lang=EN-US>A(a<sub>1</sub>a<sub>2</sub>a<sub>3</sub> &#8230; a<sub>m</sub>)</span>的前<span lang=EN-US>i</span>个字符（即为<span lang=EN-US>a<sub>1</sub>,a<sub>2</sub>,a<sub>3</sub> &#8230; a<sub>i</sub></span>）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">设<span lang=EN-US>B<sub>j</sub></span>为字符串<span lang=EN-US>B(b<sub>1</sub>b<sub>2</sub>b<sub>3</sub> &#8230; b<sub>n</sub>)</span>的前<span lang=EN-US>j</span>个字符（即为<span lang=EN-US>b<sub>1</sub>,b<sub>2</sub>,b<sub>3</sub> &#8230; b<sub>j</sub></span>）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">设<span lang=EN-US> L(i , j)</span>为使两个字符串和<span lang=EN-US>A<sub>i</sub></span>和<span lang=EN-US>B<sub>j</sub></span>相等的最小操作次数。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">当<span style="COLOR: blue" lang=EN-US>a<sub>i</sub></span><span style="COLOR: blue">等于<span lang=EN-US>b<sub>j</sub></span></span>时 显然<span style="COLOR: blue" lang=EN-US>L(i, j)=L(i-1, j-1)</span><span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">当<span style="COLOR: blue" lang=EN-US>a<sub>i</sub></span><span style="COLOR: blue">不等于<span lang=EN-US>b<sub>j</sub></span></span>时 <span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><span style="mso-spacerun: yes">&nbsp; </span></span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">若将它们修改为相等，则对两个字符串至少还要操作<span lang=EN-US>L(i-1, j-1)</span>次<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><span style="mso-spacerun: yes">&nbsp; </span></span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">若删除<span lang=EN-US>a<sub>i</sub></span>或在<span lang=EN-US>B<sub>j</sub></span>后添加<span lang=EN-US>a<sub>i</sub></span>，则对两个字符串至少还要操作<span lang=EN-US>L(i-1, j)</span>次<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><span style="mso-spacerun: yes">&nbsp; </span></span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">若删除<span lang=EN-US>b<sub>j</sub></span>或在<span lang=EN-US>A<sub>i</sub></span>后添加<span lang=EN-US>b<sub>j</sub></span>，则对两个字符串至少还要操作<span lang=EN-US>L(i, j-1)</span>次<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><span style="mso-spacerun: yes">&nbsp; </span></span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">此时<span style="COLOR: blue" lang=EN-US>L(i, j)=min( L(i-1, j-1), L(i-1, j), L(i, j-1) ) <span style="mso-spacerun: yes">&nbsp;</span>+ 1</span><span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">显然，<span lang=EN-US>L(i, 0)=i</span>，<span lang=EN-US>L(0, j)=j, </span>再利用上述的递推公式，可以直接计算出<span lang=EN-US>L(i, j)</span>值。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 15.75pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none; mso-char-indent-count: 1.5" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体; mso-highlight: white">为了保持与书中代码一致，下面的函数参数类型是<span lang=EN-US>string,</span>而不是<span lang=EN-US>char*</span>。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 15.75pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none; mso-char-indent-count: 1.5" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体; mso-highlight: white" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_001658 onclick="this.style.display='none'; Code_Closed_Text_001658.style.display='none'; Code_Open_Image_001658.style.display='inline'; Code_Open_Text_001658.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_001658 onclick="this.style.display='none'; Code_Open_Text_001658.style.display='none'; Code_Closed_Image_001658.style.display='inline'; Code_Closed_Text_001658.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_001658>distance_1</span><span style="DISPLAY: none" id=Code_Open_Text_001658><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;string_distance(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;sa,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;sb)<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img id=Codehighlighter1_56_579_Open_Image onclick="this.style.display='none'; Codehighlighter1_56_579_Open_Text.style.display='none'; Codehighlighter1_56_579_Closed_Image.style.display='inline'; Codehighlighter1_56_579_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_56_579_Closed_Image onclick="this.style.display='none'; Codehighlighter1_56_579_Closed_Text.style.display='none'; Codehighlighter1_56_579_Open_Image.style.display='inline'; Codehighlighter1_56_579_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_56_579_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_56_579_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sz_a</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sa.size()</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sz_b</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sb.size()</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j,k,tmp;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;arr(sz_a,&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">(sz_b)&nbsp;);<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sz_a;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)&nbsp;arr[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sz_b;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)&nbsp;arr[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img id=Codehighlighter1_291_547_Open_Image onclick="this.style.display='none'; Codehighlighter1_291_547_Open_Text.style.display='none'; Codehighlighter1_291_547_Closed_Image.style.display='inline'; Codehighlighter1_291_547_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_291_547_Closed_Image onclick="this.style.display='none'; Codehighlighter1_291_547_Closed_Text.style.display='none'; Codehighlighter1_291_547_Open_Image.style.display='inline'; Codehighlighter1_291_547_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sz_a;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_291_547_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_291_547_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img id=Codehighlighter1_319_543_Open_Image onclick="this.style.display='none'; Codehighlighter1_319_543_Open_Text.style.display='none'; Codehighlighter1_319_543_Closed_Image.style.display='inline'; Codehighlighter1_319_543_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_319_543_Closed_Image onclick="this.style.display='none'; Codehighlighter1_319_543_Closed_Text.style.display='none'; Codehighlighter1_319_543_Open_Image.style.display='inline'; Codehighlighter1_319_543_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sz_b;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_319_543_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_319_543_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;sa[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;sb[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])&nbsp;arr[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_386_537_Open_Image onclick="this.style.display='none'; Codehighlighter1_386_537_Open_Text.style.display='none'; Codehighlighter1_386_537_Closed_Image.style.display='inline'; Codehighlighter1_386_537_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_386_537_Closed_Image onclick="this.style.display='none'; Codehighlighter1_386_537_Closed_Text.style.display='none'; Codehighlighter1_386_537_Open_Image.style.display='inline'; Codehighlighter1_386_537_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_386_537_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_386_537_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;arr[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][j]&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;arr[i][j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">&nbsp;arr[i][j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]&nbsp;:&nbsp;arr[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][j];<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(tmp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">arr[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tmp</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;arr[sz_a</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">][sz_b</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></span></div>
<br><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; FONT-SIZE: 10.5pt; mso-bidi-font-family: 新宋体; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">由于只要求计算两字串的距离，计算时，只用到两列数据，因而可以对代码进一步优化，节省空间。<br><br><br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_001748 onclick="this.style.display='none'; Code_Closed_Text_001748.style.display='none'; Code_Open_Image_001748.style.display='inline'; Code_Open_Text_001748.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_001748 onclick="this.style.display='none'; Code_Open_Text_001748.style.display='none'; Code_Closed_Image_001748.style.display='inline'; Code_Closed_Text_001748.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_001748>distance_2</span><span style="DISPLAY: none" id=Code_Open_Text_001748><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;string_distance2(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;sa,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;sb)<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img id=Codehighlighter1_57_765_Open_Image onclick="this.style.display='none'; Codehighlighter1_57_765_Open_Text.style.display='none'; Codehighlighter1_57_765_Closed_Image.style.display='inline'; Codehighlighter1_57_765_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_57_765_Closed_Image onclick="this.style.display='none'; Codehighlighter1_57_765_Closed_Text.style.display='none'; Codehighlighter1_57_765_Open_Image.style.display='inline'; Codehighlighter1_57_765_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_57_765_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_57_765_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sz_a</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sa.size()</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sz_b</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sb.size()</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sz_max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sz_a;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sz_min</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sz_b;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">longer</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sa.data();<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">shorter</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sb.data();<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img id=Codehighlighter1_240_323_Open_Image onclick="this.style.display='none'; Codehighlighter1_240_323_Open_Text.style.display='none'; Codehighlighter1_240_323_Closed_Image.style.display='inline'; Codehighlighter1_240_323_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_240_323_Closed_Image onclick="this.style.display='none'; Codehighlighter1_240_323_Closed_Text.style.display='none'; Codehighlighter1_240_323_Open_Image.style.display='inline'; Codehighlighter1_240_323_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(sz_a&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;sz_b)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_240_323_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_240_323_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;sz_max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sz_b;<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;sz_min</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sz_a;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;longer</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sb.data();<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;shorter</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sa.data();<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j,k,tmp;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;&nbsp;arr(sz_min</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sz_min;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)&nbsp;arr[j</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">j;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_438_741_Open_Image onclick="this.style.display='none'; Codehighlighter1_438_741_Open_Text.style.display='none'; Codehighlighter1_438_741_Closed_Image.style.display='inline'; Codehighlighter1_438_741_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_438_741_Closed_Image onclick="this.style.display='none'; Codehighlighter1_438_741_Closed_Text.style.display='none'; Codehighlighter1_438_741_Open_Image.style.display='inline'; Codehighlighter1_438_741_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sz_max;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_438_741_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_438_741_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;arr[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img id=Codehighlighter1_482_689_Open_Image onclick="this.style.display='none'; Codehighlighter1_482_689_Open_Text.style.display='none'; Codehighlighter1_482_689_Closed_Image.style.display='inline'; Codehighlighter1_482_689_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_482_689_Closed_Image onclick="this.style.display='none'; Codehighlighter1_482_689_Closed_Text.style.display='none'; Codehighlighter1_482_689_Open_Image.style.display='inline'; Codehighlighter1_482_689_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sz_min;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_482_689_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_482_689_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img id=Codehighlighter1_523_683_Open_Image onclick="this.style.display='none'; Codehighlighter1_523_683_Open_Text.style.display='none'; Codehighlighter1_523_683_Closed_Image.style.display='inline'; Codehighlighter1_523_683_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_523_683_Closed_Image onclick="this.style.display='none'; Codehighlighter1_523_683_Closed_Text.style.display='none'; Codehighlighter1_523_683_Open_Image.style.display='inline'; Codehighlighter1_523_683_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;longer[i</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;shorter[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_523_683_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_523_683_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;arr[j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;arr[j]&nbsp;</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">&nbsp;arr[j]&nbsp;:&nbsp;arr[j</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(tmp</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">arr[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])&nbsp;tmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">tmp</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">tmp=&nbsp;min(arr[j-1],arr[j],arr[j+1])</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">26</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sz_min</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">j)&nbsp;arr[j</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">arr[j];<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;arr[sz_min];<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></span></div>
<br>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体; mso-highlight: white" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none; mso-char-indent-count: 2.0" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体; mso-highlight: white">上面的代码还可进一步优化，比如通过指针而不是数组名来访问内存。如果内存足够大，可以多申请空间，每次循环，通过修改保存的数据起始位置，避免内存复制。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;<br><br></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">补充：字符串的相似度，就是求编辑距离（<span lang=EN-US>edit distance</span>）。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><br></o:p></span></p>
<br></span>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123542.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:21 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123542.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记10： 2.18 数组分割</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123541.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:16:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123541.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123541.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123541.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123541.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123541.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>10</span>： <span lang=EN-US>2.18 </span>数组分割<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">如果直接遍历，则至少要遍历 Cr(n-1,2*n)次（Cr(m,n)为从n个数中取m个数的组合数），为了减少遍历次数，可以先对数组排序。再将所有可能的组合大致分成几组，每个组的数组和也是升序的，通过不断的分组、查找，确定上下边界条件，最终找到所求子数组。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">如果数组各个元素均不相同，可以采用下面的算法：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">先将数组<span lang=EN-US>{a<sub>i</sub>}</span>排序，并计算出各元素的总和的一半<span lang=EN-US>S(=Sum/2.0)</span>，（对数组的划分时，可以先选中<span lang=EN-US>a<sub>0</sub></span>，再取<span lang=EN-US>n-1</span>个数）。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">假设<span lang=EN-US>T<sub>i</sub>=sum(a<sub>0</sub>+a<sub>i</sub>+a<sub>n+2</sub>+a<sub>n+3</sub>&#8230;+a<sub>2n-1</sub>) (0&lt;i&lt;=n+1) </span>则数组<span lang=EN-US>{T<sub>i</sub>}</span>也是升序。如果<span lang=EN-US>T<sub>n+1</sub>&lt;=S</span>则<span lang=EN-US>T<sub>n+1</sub></span>即为所求，如果存在<span lang=EN-US>T<sub>i</sub>=S</span>，则<span lang=EN-US>T<sub>i</sub></span>即为所求。否则，可以通过二分法找到唯一的<span lang=EN-US>i</span>、<span lang=EN-US>j</span>使得<span lang=EN-US>T<sub>i</sub>&lt;S&lt;T<sub>j</sub></span>，（选中<span lang=EN-US>a<sub>0</sub></span>和<span lang=EN-US>a<sub>j</sub></span>），记录<span lang=EN-US>T<sub>i</sub></span>，假设<span lang=EN-US>R<sub>i</sub>=sum(a<sub>0</sub>+a<sub>j</sub>+a<sub>i</sub>+a<sub>n+3</sub>+a<sub>n+4</sub>&#8230;+a<sub>2n-1</sub>) (j&lt;i&lt;=n+2)</span>，则<span lang=EN-US>T<sub>j</sub>=R<sub>n+2</sub></span>，比较<span lang=EN-US>T<sub>i</sub></span>、<span lang=EN-US>R<sub>j+1</sub></span>、<span lang=EN-US>R<sub>n+2</sub></span>，再对<span lang=EN-US>R<sub>i</sub></span>进行类似<span lang=EN-US>T<sub>i</sub></span>的分析，找出下一个数。通过不断的分组和查找和判断，最终可以找到所求的<span lang=EN-US>n</span>个数。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">例如：长度为<span lang=EN-US>8</span>的数组：共有<span lang=EN-US>35</span>种组合，对每种组合的子数组和，可以划分到几个区间：<span lang=EN-US>(</span>下面的<span lang=EN-US>0123</span>表示取<span lang=EN-US>a<sub>0</sub>+a<sub>1</sub>+a<sub>2</sub>+a<sub>3</sub>)<o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">较小值<span lang=EN-US><span style="mso-spacerun: yes">&nbsp;&nbsp; </span></span>较大值<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>0123 </span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">——<span lang=EN-US> 0167 </span>（共<span lang=EN-US>15</span>个）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>0234 </span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">——<span lang=EN-US> 0267 </span>（共<span lang=EN-US>10</span>个）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>0345 </span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">——<span lang=EN-US> 0367 </span>（共<span lang=EN-US>6</span>个）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>0456 </span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">——<span lang=EN-US> 0467 </span>（共<span lang=EN-US>3</span>个）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>0567 </span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">——<span lang=EN-US> 0567 </span>（共<span lang=EN-US>1</span>个）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>(</span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">各个较大值不必计算，它们间必然只有一个数不同（并且这个不同的数在升序数列中的位置是连续的），查找<span lang=EN-US>S</span>在哪两个较大值之间，可以用<span lang=EN-US>S</span>减去相同的数的和，得到的差去指定的范围（不同的那个数的位置范围）进行二分查找。<span lang=EN-US>)<o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">由于数组是升序，数组元素各不相同，右边的&#8220;较大值&#8221;都是升序排列且不会重复。利用数组和的一半<span lang=EN-US>S</span>进行查找，如果<span lang=EN-US>S</span>在<span lang=EN-US>0267</span>和<span lang=EN-US>0367</span>之间。只要记录<span lang=EN-US>0267</span>，并在适当时候判断该记录是否是所求的，展开<span lang=EN-US>0345</span>——<span lang=EN-US>0367</span>的<span lang=EN-US>6</span>个数，<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>0345 </span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">—— <span lang=EN-US>0347</span>（共<span lang=EN-US>3</span>个）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>0356 </span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">—— <span lang=EN-US>0357</span>（共<span lang=EN-US>2</span>个）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>0367 </span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">—— <span lang=EN-US>0367</span>（共<span lang=EN-US>1</span>个）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">再重复上述操作。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123541.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:16 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123541.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记09： 2.17 数组循环移位</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123539.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:11:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123539.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123539.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123539.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123539.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123539.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 《编程之美》读书笔记09： 2.17 数组循环移位&nbsp;对长度为n的数组(ab)左移k位，最直接的方法，就是用stl的rotate函数（stl针对三种不同的迭代器，提供了三个版本的rotate）。但在某些情况下，用stl的rotate效率极差。对数组循环，可以采用的方法有：①&nbsp;&nbsp;&nbsp;&nbsp; 动态分配一个同样长度的数组，将数据复制到该数组并改变...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2010/08/16/123539.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/123539.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:11 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123539.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记07： 2.5 寻找最大的K个数</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123538.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 16:02:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123538.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123538.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/16/123538.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123538.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123538.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 《编程之美》读书笔记07： 2.5 寻找最大的K个数&nbsp;问题：从n个数中找出最大的k个数。&nbsp;两种思路：1 保存目前找到的最大k个数，每访问一个数，就与这k个数中的最小值比较，决定是否更新这k个数。储存k个数的数据结构可采用：败者树、二叉查找树、最小堆。C++ STL提供了multiset和priority_queue容器，另外还提供了make_heap...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2010/08/16/123538.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/123538.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-16 00:02 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/16/123538.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记06： 1.13 NIM(3)两堆石头的游戏</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123537.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 15:58:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123537.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123537.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123537.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123537.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123537.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 《编程之美》读书笔记06： 1.13 NIM(3)两堆石头的游戏&nbsp;问题：假设有两堆石子，两人轮流取石子，每次可以从一堆取任意个石子，或者从两堆取相等数量的任意个石子，但不能不取。若先把石子取光的一方为胜方，先取者有什么必胜策略？若先把石子取光的一方为输方，先取者的策略要进行怎样调整？&nbsp;记得初中时在学校门口的书店，买到一本《智力游戏中的数学方法》，当时...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2010/08/15/123537.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/123537.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-15 23:58 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123537.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记05： 1.9 高效率的安排见面会</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123536.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 15:55:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123536.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123536.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123536.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123536.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123536.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>05</span>： <span lang=EN-US>1.9 </span>高效率的安排见面会<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">扩展问题一：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">实际上就是求区间的最大重叠次数。书上<span lang=EN-US>P57</span>的算法，比较巧妙，但要注意的是：排序时要用到双关键字比较，当两个值相等时，属于时间段开始的一定要排在属于时间段结束的后面，只有这样才能保证结果的正确性。（假设<span lang=EN-US>[3, 4</span>）和<span lang=EN-US>[4, 5)</span>能在同一个地方举行。书上区间段都是用闭区间，本文采用前闭后开。）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21.75pt; MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">考虑到面试安排的时间一般安排在某个整点、半点或者某刻，可以采用计数的方法，如果都安排在整点，每处理一个区间<span lang=EN-US>[a, b)</span>，就对<span lang=EN-US>[a, b)</span>间的所有整数计数一次。最后从计数结果中找出最大值即可。时间复杂度为<span lang=EN-US>O(n)</span>（准确的讲，应该是<span lang=EN-US>O(k*n)</span>，<span lang=EN-US>k</span>为区间的最大间隔，<span lang=EN-US>k&lt;=24</span>）。如果面试安排时间在某个半点、刻，可以对原来的时间乘以一个整数（比如<span lang=EN-US>2</span>或<span lang=EN-US>4</span>，这实际上就是桶排序设置桶间隔为<span lang=EN-US>0.5</span>或<span lang=EN-US>0.25</span>）。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><br><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体; mso-highlight: white" lang=EN-US><o:p></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">arr[][0]为面试开始时间，arr[][1]为面试结束时间</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;max_places&nbsp;(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;arr[][</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">],&nbsp;size_t&nbsp;sz)<br><img id=Codehighlighter1_76_360_Open_Image onclick="this.style.display='none'; Codehighlighter1_76_360_Open_Text.style.display='none'; Codehighlighter1_76_360_Closed_Image.style.display='inline'; Codehighlighter1_76_360_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_76_360_Closed_Image onclick="this.style.display='none'; Codehighlighter1_76_360_Closed_Text.style.display='none'; Codehighlighter1_76_360_Open_Image.style.display='inline'; Codehighlighter1_76_360_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_76_360_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_76_360_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(arr</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">NULL&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;sz</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;size_t&nbsp;MAX_HOURS</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">24</span><span style="COLOR: #000000">;<br><img id=Codehighlighter1_166_168_Open_Image onclick="this.style.display='none'; Codehighlighter1_166_168_Open_Text.style.display='none'; Codehighlighter1_166_168_Closed_Image.style.display='inline'; Codehighlighter1_166_168_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_166_168_Closed_Image onclick="this.style.display='none'; Codehighlighter1_166_168_Closed_Text.style.display='none'; Codehighlighter1_166_168_Open_Image.style.display='inline'; Codehighlighter1_166_168_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;count[MAX_HOURS]</span><span style="COLOR: #000000">=</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_166_168_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_166_168_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">}</span></span><span style="COLOR: #000000">;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;size_t&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sz;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr[i][</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];&nbsp;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">arr[i][</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">count[j];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">MAX_HOURS;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(count[i]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">max)&nbsp;max</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">count[i];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;max;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<br></o:p></span>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123536.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-15 23:55 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123536.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记04： 1.8 小飞的电梯调度算法</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123535.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 15:54:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123535.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123535.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123535.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123535.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123535.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>04</span>： <span lang=EN-US>1.8 </span>小飞的电梯调度算法<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">假设电梯有<span lang=EN-US>n</span>层，上楼要消耗能量<span lang=EN-US>k<sub>1</sub></span>，下楼要消耗能量<span lang=EN-US>k<sub>2</sub></span>，用<span lang=EN-US>a[i]</span>表示要在第<span lang=EN-US>i</span>层下的人数，<span lang=EN-US>S<sub>i</sub></span>为到<span lang=EN-US>i</span>层时已经下（包括<span lang=EN-US>i</span>层）的总人数，则总人数<span lang=EN-US>S=S<sub>n</sub></span>。若用<span lang=EN-US>F(i)</span>表示电梯在<span lang=EN-US>i</span>层停时要消耗的总能量，则电梯在<span lang=EN-US>i+1</span>层停时，有<span lang=EN-US>S<sub>i</sub></span>人要多下一层，（<span lang=EN-US>S-S<sub>i</sub></span>）人少上一层。则：<span lang=EN-US><span style="mso-spacerun: yes">&nbsp;&nbsp; </span><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 42pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 4.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>F(i+1) = F(i) + k<sub>2</sub>*S<sub>i</sub> - k<sub>1</sub>*(S-S<sub>i</sub>) = F(i) + (k<sub>2</sub>+k<sub>1</sub>)*S<sub>i</sub> &#8211; k<sub>1</sub>*S </span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">＝<span lang=EN-US> F(i) + G(i) <o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 42pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 4.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">（定义<span lang=EN-US>G(i) = (k<sub>2</sub>+k<sub>1</sub>)*S<sub>i</sub> &#8211; k<sub>1</sub>*S</span>）<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">由于<span lang=EN-US>S<sub>i</sub></span>是递增的，<span lang=EN-US>G(i)</span>也是递增的，当<span lang=EN-US>G(i) &lt;= 0</span>，<span lang=EN-US>F(i+1) &lt;= F(i)</span>，&#8220;求使<span lang=EN-US>F(i)</span>最小的<span lang=EN-US>i</span>&#8221;问题等同于 &#8220;<span style="COLOR: blue">求使<span lang=EN-US>G(i)</span>＝<span lang=EN-US>(k<sub>2</sub>+k<sub>1</sub>)*S<sub>i</sub> &#8211; k<sub>1</sub>*S<span style="mso-spacerun: yes">&nbsp; </span>&lt;= 0</span>的最大<span lang=EN-US>i</span>值</span>&#8221;（所得<span lang=EN-US>i</span>值<span lang=EN-US>+1</span>即为原问题的解），或 &#8220;<span style="COLOR: blue">求使<span lang=EN-US>G(i)</span>＝<span lang=EN-US>(k<sub>2</sub>+k<sub>1</sub>)*S<sub>i</sub> &#8211; k<sub>1</sub>*S<span style="mso-spacerun: yes">&nbsp; </span>&gt;= 0</span>的最小<span lang=EN-US>i</span>值</span>&#8221;（所得<span lang=EN-US>i</span>值即为原问题的解）。注意：等号可取可不取。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">对书上原题：<span lang=EN-US>k<sub>1</sub>=k<sub>2</sub>=1</span>，<span style="COLOR: blue" lang=EN-US>G(i)=2*S<sub>i </sub>&#8211; S &gt;= 0</span>，可以扫描数组两遍，第一遍算出<span lang=EN-US>S</span>，第二遍算出使<span lang=EN-US> 2*S<sub>i </sub>&#8211; S &lt; 0 </span>的最大<span lang=EN-US>i</span>值。也可以只扫找一遍，用两个指针分别指向数组的开头和结尾，一个向前移动，一个向后移动，并同时开始计算最前几个数的和<span lang=EN-US>S<sub>2, i</sub></span>和最后几个数的和<span lang=EN-US>S<sub>j, n</sub></span>，通过调整两个指针位置，使<span lang=EN-US>S<sub>2, i</sub>&lt;= S<sub>j, n</sub></span>总成立并使<span lang=EN-US>i</span>尽可能的大，这样扫描完毕，<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US>2*S<sub>2, i </sub>&lt;= S<sub>2, i </sub>+ S<sub>i+1, n</sub> = S</span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">，且<span lang=EN-US> 2*S<sub>2, i+1</sub> &gt;= S</span>。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">（书中解法二的分析与给出的代码不对应，只有证明&#8220;使<span lang=EN-US>N1 + N2 &gt;= N3</span>成立的第一个<span lang=EN-US>i</span>值就是全局最优解&#8221;，才能保证给出的代码的正确性。）<br><br><br></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img id=Code_Closed_Image_235335 onclick="this.style.display='none'; Code_Closed_Text_235335.style.display='none'; Code_Open_Image_235335.style.display='inline'; Code_Open_Text_235335.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 height=16><img style="DISPLAY: none" id=Code_Open_Image_235335 onclick="this.style.display='none'; Code_Open_Text_235335.style.display='none'; Code_Closed_Image_235335.style.display='inline'; Code_Closed_Text_235335.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 height=16><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Code_Closed_Text_235335>程序代码</span><span style="DISPLAY: none" id=Code_Open_Text_235335><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">//</span><span style="COLOR: #008000">arr[i]&nbsp;为在第i层要下的人数，因而i&gt;=2;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;lift(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">arr,&nbsp;unsigned&nbsp;sz,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;total)<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img id=Codehighlighter1_72_343_Open_Image onclick="this.style.display='none'; Codehighlighter1_72_343_Open_Text.style.display='none'; Codehighlighter1_72_343_Closed_Image.style.display='inline'; Codehighlighter1_72_343_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_72_343_Closed_Image onclick="this.style.display='none'; Codehighlighter1_72_343_Closed_Text.style.display='none'; Codehighlighter1_72_343_Open_Image.style.display='inline'; Codehighlighter1_72_343_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_72_343_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_72_343_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;assert(sz</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,&nbsp;sum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;count</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;total</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_149_195_Open_Image onclick="this.style.display='none'; Codehighlighter1_149_195_Open_Text.style.display='none'; Codehighlighter1_149_195_Closed_Image.style.display='inline'; Codehighlighter1_149_195_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_149_195_Closed_Image onclick="this.style.display='none'; Codehighlighter1_149_195_Closed_Text.style.display='none'; Codehighlighter1_149_195_Open_Image.style.display='inline'; Codehighlighter1_149_195_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">sz;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_149_195_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_149_195_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;arr[i];<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;total&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;arr[i]</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">i;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;total&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;total&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">&nbsp;sum&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img id=Codehighlighter1_243_329_Open_Image onclick="this.style.display='none'; Codehighlighter1_243_329_Open_Text.style.display='none'; Codehighlighter1_243_329_Closed_Image.style.display='inline'; Codehighlighter1_243_329_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_243_329_Closed_Image onclick="this.style.display='none'; Codehighlighter1_243_329_Closed_Text.style.display='none'; Codehighlighter1_243_329_Open_Image.style.display='inline'; Codehighlighter1_243_329_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;&nbsp;;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_243_329_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_243_329_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;count&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;arr[i]&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(&nbsp;count&nbsp;</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">&nbsp;sum&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;total&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;count&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">&nbsp;sum;<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;i;<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">arr[i]&nbsp;为在第i层要下的人数，因而i&gt;=2;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;lift2(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">arr,&nbsp;unsigned&nbsp;sz,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;total)<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img id=Codehighlighter1_422_845_Open_Image onclick="this.style.display='none'; Codehighlighter1_422_845_Open_Text.style.display='none'; Codehighlighter1_422_845_Closed_Image.style.display='inline'; Codehighlighter1_422_845_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_422_845_Closed_Image onclick="this.style.display='none'; Codehighlighter1_422_845_Closed_Text.style.display='none'; Codehighlighter1_422_845_Open_Image.style.display='inline'; Codehighlighter1_422_845_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_422_845_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_422_845_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;assert(sz</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">low</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">high</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">arr</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">sz</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;sum_a</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;sum_b</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;sum_ta</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,&nbsp;sum_tb</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img id=Codehighlighter1_540_680_Open_Image onclick="this.style.display='none'; Codehighlighter1_540_680_Open_Text.style.display='none'; Codehighlighter1_540_680_Closed_Image.style.display='inline'; Codehighlighter1_540_680_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_540_680_Closed_Image onclick="this.style.display='none'; Codehighlighter1_540_680_Closed_Text.style.display='none'; Codehighlighter1_540_680_Open_Image.style.display='inline'; Codehighlighter1_540_680_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(low&nbsp;</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">&nbsp;high)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_540_680_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_540_680_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img id=Codehighlighter1_565_617_Open_Image onclick="this.style.display='none'; Codehighlighter1_565_617_Open_Text.style.display='none'; Codehighlighter1_565_617_Closed_Image.style.display='inline'; Codehighlighter1_565_617_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_565_617_Closed_Image onclick="this.style.display='none'; Codehighlighter1_565_617_Closed_Text.style.display='none'; Codehighlighter1_565_617_Open_Image.style.display='inline'; Codehighlighter1_565_617_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(sum_a&nbsp;</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">&nbsp;sum_b)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_565_617_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_565_617_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum_a&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">low</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum_ta&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;sum_a;<br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img id=Codehighlighter1_623_676_Open_Image onclick="this.style.display='none'; Codehighlighter1_623_676_Open_Text.style.display='none'; Codehighlighter1_623_676_Closed_Image.style.display='inline'; Codehighlighter1_623_676_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_623_676_Closed_Image onclick="this.style.display='none'; Codehighlighter1_623_676_Closed_Text.style.display='none'; Codehighlighter1_623_676_Open_Image.style.display='inline'; Codehighlighter1_623_676_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_623_676_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_623_676_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum_b&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">high</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum_tb&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;sum_b;<br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">low;<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">电梯所停的那层始终被计算了,要扣除</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">40</span><span style="COLOR: #008000"><img id=Codehighlighter1_732_757_Open_Image onclick="this.style.display='none'; Codehighlighter1_732_757_Open_Text.style.display='none'; Codehighlighter1_732_757_Closed_Image.style.display='inline'; Codehighlighter1_732_757_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_732_757_Closed_Image onclick="this.style.display='none'; Codehighlighter1_732_757_Closed_Text.style.display='none'; Codehighlighter1_732_757_Open_Image.style.display='inline'; Codehighlighter1_732_757_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(sum_a&nbsp;</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">&nbsp;sum_b)</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_732_757_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_732_757_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;sum_ta&nbsp;</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">&nbsp;sum_a;<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img id=Codehighlighter1_763_795_Open_Image onclick="this.style.display='none'; Codehighlighter1_763_795_Open_Text.style.display='none'; Codehighlighter1_763_795_Closed_Image.style.display='inline'; Codehighlighter1_763_795_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_763_795_Closed_Image onclick="this.style.display='none'; Codehighlighter1_763_795_Closed_Text.style.display='none'; Codehighlighter1_763_795_Open_Image.style.display='inline'; Codehighlighter1_763_795_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_763_795_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_763_795_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">low;<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">sum_tb&nbsp;</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">&nbsp;sum_b;<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;total&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;sum_ta&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;sum_tb;<br></span><span style="COLOR: #008080">48</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;low&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">&nbsp;arr;<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">50</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></span></div>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><br><span lang=EN-US><o:p></o:p></span></span></p>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123535.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-15 23:54 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123535.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记03： 1.4 买书问题</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123534.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 15:48:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123534.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123534.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123534.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123534.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123534.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 《编程之美》读书笔记03： 1.4 买书问题&nbsp;问题：&nbsp;&nbsp;&nbsp; 在节假日的时候，书店一般都会做促销活动。由于《哈利波特》系列相当畅销，店长决定通过促销活动来回馈读者。在销售的《哈利波特》平装本系列中，一共有五卷，用编号0, 1, 2, 3, 4来表示。假设每一卷单独销售均需要8欧元。如果读者一次购买不同的两卷，就可以扣除5%的费用，三卷则更多。假设具...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2010/08/15/123534.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/123534.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-15 23:48 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123534.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记01： 1.2中国象棋将帅问题</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123533.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 15:41:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123533.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123533.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123533.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123533.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123533.html</trackback:ping><description><![CDATA[<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">《编程之美》读书笔记<span lang=EN-US>01</span>： <span lang=EN-US>1.2</span>中国象棋将帅问题<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">当初，刚看到题时，首先想到的是除法计算除数的商和余数（<span lang=EN-US>eax</span>和<span lang=EN-US>edx</span>）。后来才想到需要动态多维数组时，<span lang=EN-US>new</span>一维数组，用它模拟多维数组时，多维数组的下标和实际偏移量的转换，我想很多人学习<span lang=EN-US>C</span>或<span lang=EN-US>C++</span>时都做过这种事吧。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">本来以为题目要求输出将帅在棋盘上的具体位置，如<span lang=EN-US>d10</span>、<span lang=EN-US>f1</span>，书中的解法给的是相对位置，解决起来更简单。解法一用了一堆令人讨厌的宏，代码实在不美，解法三和解法一本质是一样的，虽然解法三只定义了一个结构体，但结构体内有两个变量，总共有三个变量，不合题意才对。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">要将一个变量<span lang=EN-US>i</span>拆成两个，可以按其的二进制表示，取出连续几位，比如第<span lang=EN-US>0-3</span>位和第<span lang=EN-US>4-7</span>位，读变量时，取出变量<span lang=EN-US>i</span>相应的几位，存变量时，再更新变量<span lang=EN-US>i</span>的对应几位。另外，利用位置的对称性，可以一次输出两个，减少循环次数。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-char-indent-count: 2.0" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">下面的代码和解法一类似，但是一次输出两个，减少了循环次数，并且没有用到除法，如果不考虑<span lang=EN-US>C++ IO</span>效率的影响，会比解法二和解法三都高效。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;<br></o:p></span></p>
<p style="MARGIN: 0cm 0cm 0pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt" lang=EN-US><o:p>&nbsp;</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;unsigned&nbsp;i;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">外层循环变量a使用i的第4-7位，初始值为1，最大值为8。<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">内层循环变量b使用i的第0-3位，初始值为a+1，最大值为9。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0x10</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">0x90</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">0x10</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">0xF0</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">);&nbsp;(</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">0xF</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;&nbsp;)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(&nbsp;((i</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">0xF</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">))</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;((i</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">0xF</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">))</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">)<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">A=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;B=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">0xF</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">A=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">0xF</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;B=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;(i</span><span style="COLOR: #000000">&gt;&gt;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
<br></o:p></span>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/123533.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-15 23:41 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123533.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记22：    1.16  24点游戏（补充）</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123531.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 15:35:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123531.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123531.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123531.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123531.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123531.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 《编程之美》读书笔记22：&nbsp;&nbsp;&nbsp; 1.16&nbsp; 24点游戏（补充）&nbsp;给定n个数，能否只通过加减乘除计算得到24？ 书上给出的最后一种解法，通过使用集合记录中间结果来减少冗余计算。本以为，程序会占用大量的内存，用一个极端的例子（13, 773, 28, 98, 731, 1357,97357246这7个数）测试了一下实现的程序，发现程序竟然...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2010/08/15/123531.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/123531.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-15 23:35 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123531.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>N个数计算24点</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123529.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 15 Aug 2010 15:20:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123529.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/123529.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/15/123529.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/123529.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/123529.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: N个数计算24点问题：&nbsp;&nbsp;&nbsp; N个1到13之间的自然数，找出所有能通过加减乘除计算（每个数有且只能用一次）得到24的组合？&nbsp;计算24点常用的算法有：① 任取两个数，计算后，将结果放回去，再从剩下的数中任取两个，如此反复直到只剩下一个数；② 先构建前缀/后缀表达式，再计算该表达式；③ 用集合保存中间结果，集合间两两进行合并计算得到新集合（或者对...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2010/08/15/123529.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/123529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-15 23:20 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/15/123529.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记22：    1.16  24点游戏</title><link>http://www.cppblog.com/flyinghearts/archive/2010/08/01/121907.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Sun, 01 Aug 2010 14:50:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/08/01/121907.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/121907.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/08/01/121907.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/121907.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/121907.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 《编程之美》读书笔记22：&nbsp;&nbsp;&nbsp; 1.16&nbsp; 24点游戏给定4个数，能否只通过加减乘除计算得到24？由于只有4个数，弄个多重循环，就可以。如果要推广到n个数，有两种思路：① 采用前缀/后缀表达式。相当于将n个数用n-1个括号括起来，其数目就是一个catlan数。最多可得到 f(n) = (1/n * (2*n - 2)! / (n-1)! / (...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2010/08/01/121907.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/121907.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-08-01 22:50 <a href="http://www.cppblog.com/flyinghearts/archive/2010/08/01/121907.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记21： 2.4  1的数目</title><link>http://www.cppblog.com/flyinghearts/archive/2010/07/21/120915.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Tue, 20 Jul 2010 16:25:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/07/21/120915.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/120915.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/07/21/120915.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/120915.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/120915.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;《编程之美》读书笔记21： 2.4 1的数目&nbsp;问题：&nbsp;&nbsp;&nbsp; 给定一个十进制正整数N，写下从1开始，到N的所有整数，&nbsp;&nbsp;&nbsp; 然后数一下其中出现的所有&#8220;1&#8221;的个数。 &nbsp; &nbsp;&nbsp;例如： &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;...&nbsp;&nbsp;<a href='http://www.cppblog.com/flyinghearts/archive/2010/07/21/120915.html'>阅读全文</a><img src ="http://www.cppblog.com/flyinghearts/aggbug/120915.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-07-21 00:25 <a href="http://www.cppblog.com/flyinghearts/archive/2010/07/21/120915.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《编程之美》读书笔记08：2.9 Fibonacci序列 —— O(log n)求Fibonacci数列（非矩阵法）</title><link>http://www.cppblog.com/flyinghearts/archive/2010/06/23/118593.html</link><dc:creator>flyinghearts</dc:creator><author>flyinghearts</author><pubDate>Wed, 23 Jun 2010 15:28:00 GMT</pubDate><guid>http://www.cppblog.com/flyinghearts/archive/2010/06/23/118593.html</guid><wfw:comment>http://www.cppblog.com/flyinghearts/comments/118593.html</wfw:comment><comments>http://www.cppblog.com/flyinghearts/archive/2010/06/23/118593.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cppblog.com/flyinghearts/comments/commentRss/118593.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyinghearts/services/trackbacks/118593.html</trackback:ping><description><![CDATA[<p style="TEXT-ALIGN: left" class=MsoNormal align=left>《编程之美》读书笔记08：2.9 Fibonacci序列 <br><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black"></p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体; mso-highlight: white" lang=EN-US><o:p></o:p></span></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none; mso-char-indent-count: 2.0" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体; mso-highlight: white">计算</span><span style="FONT-FAMILY: 宋体; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体" lang=EN-US>Fibonacci</span><span style="FONT-FAMILY: 宋体; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体">序列最直接的方法就是利用递推公式<span lang=EN-US> F(n+2)=F(n+1)+F(n)</span>。而用通项公式来求解是错误的，用浮点数表示无理数本来就有误差，经过<span lang=EN-US>n</span>次方后，当<span lang=EN-US>n</span>相当大时，误差能足够大到影响浮点数转为整数时的精度，得到的结果根本不准。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none; mso-char-indent-count: 2.0" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体">用矩阵来计算，虽然时间复杂度降到<span lang=EN-US>O(log n)</span>，但要用到矩阵类，相当麻烦。观察：<span style="BACKGROUND: white; mso-highlight: white" lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 31.4pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none; mso-char-indent-count: 2.98" class=MsoNormal align=left><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体" lang=EN-US>F(n+2)=F(n)+F(n-1)</span></strong><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体">＝<span lang=EN-US>2*F(n-1)+F(n-2)=3*F(n-2)+2*F(n-4)<o:p></o:p></span></span></strong></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none; mso-char-indent-count: 2.0" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体">用归纳法很容易证明 </span><span style="FONT-FAMILY: 宋体; COLOR: blue; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体" lang=EN-US>F(n) = F(k)*F(n+1-k) + F(k-1)*F(n-k)</span><span style="FONT-FAMILY: 宋体; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体">，利用该递推公式和原递推公式，要计算<span lang=EN-US>F(n)</span>，只要计算<span lang=EN-US>F([n/2])</span>和<span lang=EN-US>F([n/2]+1)</span>，时间复杂度为<span lang=EN-US> O(lg n)</span>。<em style="mso-bidi-font-style: normal"><span style="BACKGROUND: white; mso-highlight: white">如：要计算<span lang=EN-US>F(58)</span>，由 <span lang=EN-US>58 -&gt; 29,30 -&gt; 14,15 -&gt; 7,8 -&gt; 3,4 -&gt; 1,2 </span>可知只要算<span lang=EN-US>5</span>次。</span></em><span style="BACKGROUND: white; mso-highlight: white">可以用一个栈保存要计算的数，实际上，将<span lang=EN-US>n</span>的最高位<span lang=EN-US>1</span>（假设在第<span lang=EN-US>k</span>位）左边的<span lang=EN-US>0</span>去除掉后，第<span lang=EN-US>m</span>次要计算的数是第<span lang=EN-US>k</span>位到第<span lang=EN-US>k-m+1</span>位这<span lang=EN-US>m</span>个位组成的值<span lang=EN-US>t(m)</span>，则第<span lang=EN-US>m-1</span>次要计算的数为<span lang=EN-US>t(m-1)</span>，且<span lang=EN-US><o:p></o:p></span></span></span></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 21pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none; mso-char-indent-count: 2.0" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体; mso-highlight: white" lang=EN-US>t(m)=2*t(m-1)+(</span><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体; mso-highlight: white">第<span lang=EN-US>k-m+1</span>位是否为<span lang=EN-US>1)</span>。</span><span style="FONT-FAMILY: 宋体; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体" lang=EN-US><o:p></o:p></span></p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">若第<span lang=EN-US>m-1</span>次计算得到了<span lang=EN-US>f(k)</span>和<span lang=EN-US>f(k+1)</span>，则第<span lang=EN-US>m</span>次计算：<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-ALIGN: left" class=MsoNormal align=left>
<table style="BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; MARGIN: auto auto auto 116.05pt; BORDER-COLLAPSE: collapse; BORDER-TOP: medium none; BORDER-RIGHT: medium none; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext" class=MsoTableGrid border=1 cellSpacing=0 cellPadding=0>
    <tbody>
        <tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes">
            <td style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: windowtext 1pt solid; PADDING-BOTTOM: 0cm; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid windowtext .5pt" vAlign=top>
            <p style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=center><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">第<span lang=EN-US>k-m+1</span>位<span lang=EN-US><o:p></o:p></span></span></p>
            </td>
            <td style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #ffffff; PADDING-BOTTOM: 0cm; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" vAlign=top>
            <p style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=center><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">已计算<span lang=EN-US><o:p></o:p></span></span></p>
            </td>
            <td style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #ffffff; PADDING-BOTTOM: 0cm; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" vAlign=top>
            <p style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=center><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">待计算<span lang=EN-US><o:p></o:p></span></span></p>
            </td>
        </tr>
        <tr style="mso-yfti-irow: 1">
            <td style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: windowtext 1pt solid; PADDING-BOTTOM: 0cm; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ffffff; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top>
            <p style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=center><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体" lang=EN-US>1<o:p></o:p></span></p>
            </td>
            <td style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #ffffff; PADDING-BOTTOM: 0cm; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ffffff; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top rowSpan=2>
            <p style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=center><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体" lang=EN-US>f(k)<o:p></o:p></span></p>
            <p style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=center><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体" lang=EN-US>f(k+1)<o:p></o:p></span></p>
            </td>
            <td style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #ffffff; PADDING-BOTTOM: 0cm; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ffffff; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top>
            <p style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=center><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体" lang=EN-US>f(2*k+1),f(2*k+2)<o:p></o:p></span></p>
            </td>
        </tr>
        <tr style="mso-yfti-irow: 2; mso-yfti-lastrow: yes">
            <td style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: windowtext 1pt solid; PADDING-BOTTOM: 0cm; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ffffff; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top>
            <p style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=center><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体" lang=EN-US>0<o:p></o:p></span></p>
            </td>
            <td style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #ffffff; PADDING-BOTTOM: 0cm; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ffffff; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top>
            <p style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=center><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体" lang=EN-US>f(2*k),f(2*k+1)<o:p></o:p></span></p>
            </td>
        </tr>
    </tbody>
</table>
</p>
<p style="TEXT-ALIGN: left; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体" lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 24pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">具体公式见下面代码。</span><span style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体" lang=EN-US><o:p></o:p></span></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 24pt; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体; mso-highlight: white">下面是计算<span lang=EN-US>F(n)</span>最后四位数（某道<span lang=EN-US>ACM</span>题）的代码。<span lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-ALIGN: left" class=MsoNormal align=left><br></span>&nbsp;</p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 24pt" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black"></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;&nbsp;&nbsp;Fibonacci数列第N个数的最后4位数<br>&nbsp;&nbsp;&nbsp;&nbsp;注意，当&nbsp;N&gt;93&nbsp;时&nbsp;第N个数的值超过64位无符号整数可表示的范围。<br>F(n+2)=F(n)+F(n-1)&nbsp;F(0)=0&nbsp;F(1)=1&nbsp;&nbsp;F(2)=1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;==&gt;<br>F(n)=F(k)*F(n+1-k)&nbsp;+&nbsp;F(k-1)*F(n-k)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;==&gt;<br>F(2*n)=F(n+1)*F(n)+F(n)*F(n-1)=(F(n+1)+F(n-1))*F(n)=(F(n+1)*2-F(n))*F(n)<br>F(2*n+1)=F(n+1)*F(n+1)+F(n)*F(n)<br>F(2*n+2)=F(n+2)*F(n+1)+F(n+1)*F(n)=(F(n+2)+F(n))*F(n+1)=(F(n+1)+F(n)*2)*F(n+1)<br>&nbsp;</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br><br>unsigned&nbsp;fib_last4(&nbsp;unsigned&nbsp;num)<br>{<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(&nbsp;num&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;unsigned&nbsp;M</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">10000</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;unsigned&nbsp;ret</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,next</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,ret_</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">ret;<br>&nbsp;&nbsp;unsigned&nbsp;flag</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;tt</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">num;<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(&nbsp;tt&nbsp;</span><span style="COLOR: #000000">&gt;&gt;=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;flag&nbsp;</span><span style="COLOR: #000000">&lt;&lt;=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(&nbsp;flag&nbsp;</span><span style="COLOR: #000000">&gt;&gt;=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(&nbsp;num&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;flag&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret_&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;next&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(ret&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;next)&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;next;<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">多加一个M，避免&nbsp;2*next-ret是负数，造成结果不对</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret_&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(next&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;next&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;M&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">&nbsp;ret)&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;ret;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;next&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;next;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;ret&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;ret_&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;M;<br>&nbsp;&nbsp;&nbsp;&nbsp;next&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;next&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;M;<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;ret;<br>}<br><br></span></div>
<br></span>
<p>&nbsp;</p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 24pt" class=MsoNormal align=left><span style="FONT-FAMILY: 宋体; BACKGROUND: white; COLOR: black"><br></span></p>
<p style="TEXT-ALIGN: left; TEXT-INDENT: 24pt" class=MsoNormal align=left><br></p>
<img src ="http://www.cppblog.com/flyinghearts/aggbug/118593.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyinghearts/" target="_blank">flyinghearts</a> 2010-06-23 23:28 <a href="http://www.cppblog.com/flyinghearts/archive/2010/06/23/118593.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>