<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GuoJing&#039;s Blog &#124; 献给未来的回忆 &#187; 趣题</title>
	<atom:link href="http://www.jguoer.com/blog/index.php/archives/category/problems/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jguoer.com/blog</link>
	<description>献给未来的回忆</description>
	<lastBuildDate>Thu, 02 Feb 2012 16:00:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>这样排序正不正确？</title>
		<link>http://www.jguoer.com/blog/index.php/archives/957</link>
		<comments>http://www.jguoer.com/blog/index.php/archives/957#comments</comments>
		<pubDate>Sat, 17 Apr 2010 14:01:03 +0000</pubDate>
		<dc:creator>诡异的西红柿</dc:creator>
				<category><![CDATA[趣题]]></category>
		<category><![CDATA[排序]]></category>
		<category><![CDATA[题目]]></category>

		<guid isPermaLink="false">http://www.jguoer.com/blog/?p=957</guid>
		<description><![CDATA[最近看到了一道非常有趣的排序问题，这个问题可以大概描述成这样，我们首先看下图。

<p style="text-align:center"><img src="http://www.jguoer.com/blog/wp-content/uploads/2010/04/funnysort.png"/></p>

这道题是这样的，在图书馆里面，有一个图书管理员需要针对一些图书进行排序，方便管理，但是排序的方法有很多，这个管理员就想到了这样一种方法，如上图所示。图书本来的序列是混乱的，最后我们期望得到的结果序列是有序的，但是这个排序的方法却和普通的排序不太相同，这个方法具体为：管理员拿出其中的一本书，然后按照这个书的序列号，放到相应的为止，其新位置的相关的书籍也随着移动（如果放到前面去了，后面的相应位置的书籍全部后移，如果放到后面去了，则前面的相应位置的书籍全部前移），一直重复这样操作，直到整个书本有序为止。

仔细看看上面的图和解释，理解这个方法应该没有多大的问题，但是问题来了，这样操作到底会不会出现死循环呢，会出现这种可能，所以到底这样排序对不对，还需要用大脑和数据来证明。

下面我给出一段代码来解释，希望能够提供到一些思路。

<div class="code">
<div><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> a[</span><span style="color: rgb(128, 0, 128);">10</span><span style="color: rgb(0, 0, 0);">]; <br />
<br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> init(); <br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> sort(); <br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> print(); <br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getright(); <br />
<br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">**</span><span style="color: rgb(0, 0, 0);"> argv) { <br />
&#160;&#160;&#160;&#160;init(); <br />
&#160;&#160;&#160;&#160;print(); <br />
&#160;&#160;&#160;&#160;cout</span><span style="color: rgb(0, 0, 0);">&#60;&#60;</span><span style="color: rgb(128, 0, 0);">&#34;</span><span style="color: rgb(128, 0, 0);">============</span><span style="color: rgb(128, 0, 0);">&#34;</span><span style="color: rgb(0, 0, 0);">&#60;&#60;</span><span style="color: rgb(0, 0, 0);">endl; <br />
&#160;&#160;&#160;&#160;sort(); <br />
&#160;&#160;&#160;&#160;print(); <br />
&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">; <br />
} <br />
<br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> init(){ <br />
&#160;&#160;&#160;&#160;a[</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">3</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;a[</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;a[</span><span style="color: rgb(128, 0, 128);">2</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">2</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;a[</span><span style="color: rgb(128, 0, 128);">3</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">5</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;a[</span><span style="color: rgb(128, 0, 128);">4</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">4</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;a[</span><span style="color: rgb(128, 0, 128);">5</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">9</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;a[</span><span style="color: rgb(128, 0, 128);">6</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">7</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;a[</span><span style="color: rgb(128, 0, 128);">7</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">8</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;a[</span><span style="color: rgb(128, 0, 128);">8</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">6</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;a[</span><span style="color: rgb(128, 0, 128);">9</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">; <br />
} <br />
<br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getright(){ <br />
&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&#60;</span><span style="color: rgb(128, 0, 128);">10</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">){ <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(a[i]</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">i) <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">; <br />
&#160;&#160;&#160;&#160;} <br />
&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">; <br />
} <br />
<br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> sort(){ <br />
&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">getright()) <br />
&#160;&#160;&#160;&#160;{ <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&#60;</span><span style="color: rgb(128, 0, 128);">10</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">) <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{ <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> value </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> a[i]; <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> index </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i; <br />
<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(value</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">index) <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);">; <br />
<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">value大于index</span><span style="color: rgb(0, 128, 0);"> <br />
</span><span style="color: rgb(0, 0, 0);">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(value</span><span style="color: rgb(0, 0, 0);">&#62;</span><span style="color: rgb(0, 0, 0);">index) <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{ <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> temp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> value; <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">index;j</span><span style="color: rgb(0, 0, 0);">&#60;</span><span style="color: rgb(0, 0, 0);">value;j</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">) <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{ <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;a[j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">a[j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">]; <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;} <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;a[temp]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">temp; <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;} <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(value</span><span style="color: rgb(0, 0, 0);">&#60;</span><span style="color: rgb(0, 0, 0);">index) <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{ <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> temp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> value; <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">index;j</span><span style="color: rgb(0, 0, 0);">&#62;</span><span style="color: rgb(0, 0, 0);">value;j</span><span style="color: rgb(0, 0, 0);">--</span><span style="color: rgb(0, 0, 0);">) <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{ <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;a[j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">a[j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">]; <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;} <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;a[temp]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">temp; <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;} <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;} <br />
&#160;&#160;&#160;&#160;} <br />
&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">; <br />
} <br />
<br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> print(){ <br />
&#160;&#160;&#160;&#160;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&#60;</span><span style="color: rgb(128, 0, 128);">10</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">) <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cout</span><span style="color: rgb(0, 0, 0);">&#60;&#60;</span><span style="color: rgb(0, 0, 0);">a[i]</span><span style="color: rgb(0, 0, 0);">&#60;&#60;</span><span style="color: rgb(0, 0, 0);">endl; <br />
}</span></div>
</div>]]></description>
			<content:encoded><![CDATA[<p>最近看到了一道非常有趣的排序问题，这个问题可以大概描述成这样，我们首先看下图。</p>
<p style="text-align:center"><img src="http://www.jguoer.com/blog/wp-content/uploads/2010/04/funnysort.png"/></p>
<p>这道题是这样的，在图书馆里面，有一个图书管理员需要针对一些图书进行排序，方便管理，但是排序的方法有很多，这个管理员就想到了这样一种方法，如上图所示。图书本来的序列是混乱的，最后我们期望得到的结果序列是有序的，但是这个排序的方法却和普通的排序不太相同，这个方法具体为：管理员拿出其中的一本书，然后按照这个书的序列号，放到相应的为止，其新位置的相关的书籍也随着移动（如果放到前面去了，后面的相应位置的书籍全部后移，如果放到后面去了，则前面的相应位置的书籍全部前移），一直重复这样操作，直到整个书本有序为止。</p>
<p>仔细看看上面的图和解释，理解这个方法应该没有多大的问题，但是问题来了，这样操作到底会不会出现死循环呢，会出现这种可能，所以到底这样排序对不对，还需要用大脑和数据来证明。</p>
<p>下面我给出一段代码来解释，希望能够提供到一些思路。</p>
<div class="code">
<div><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> a[</span><span style="color: rgb(128, 0, 128);">10</span><span style="color: rgb(0, 0, 0);">]; </p>
<p></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> init(); <br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> sort(); <br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> print(); <br />
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getright(); </p>
<p></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">**</span><span style="color: rgb(0, 0, 0);"> argv) { <br />
&nbsp;&nbsp;&nbsp;&nbsp;init(); <br />
&nbsp;&nbsp;&nbsp;&nbsp;print(); <br />
&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(128, 0, 0);">&quot;</span><span style="color: rgb(128, 0, 0);">============</span><span style="color: rgb(128, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl; <br />
&nbsp;&nbsp;&nbsp;&nbsp;sort(); <br />
&nbsp;&nbsp;&nbsp;&nbsp;print(); <br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">; <br />
} </p>
<p></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> init(){ <br />
&nbsp;&nbsp;&nbsp;&nbsp;a[</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">3</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;a[</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;a[</span><span style="color: rgb(128, 0, 128);">2</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">2</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;a[</span><span style="color: rgb(128, 0, 128);">3</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">5</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;a[</span><span style="color: rgb(128, 0, 128);">4</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">4</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;a[</span><span style="color: rgb(128, 0, 128);">5</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">9</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;a[</span><span style="color: rgb(128, 0, 128);">6</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">7</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;a[</span><span style="color: rgb(128, 0, 128);">7</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">8</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;a[</span><span style="color: rgb(128, 0, 128);">8</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">6</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;a[</span><span style="color: rgb(128, 0, 128);">9</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">; <br />
} </p>
<p></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> getright(){ <br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(128, 0, 128);">10</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">){ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(a[i]</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">i) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;} <br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">; <br />
} </p>
<p></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> sort(){ <br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">getright()) <br />
&nbsp;&nbsp;&nbsp;&nbsp;{ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(128, 0, 128);">10</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> value </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> a[i]; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> index </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(value</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">index) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);">; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">value大于index</span><span style="color: rgb(0, 128, 0);"> <br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(value</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">index) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> temp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> value; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">index;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">value;j</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">a[j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">]; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[temp]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">temp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(value</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">index) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> temp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> value; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">index;j</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">value;j</span><span style="color: rgb(0, 0, 0);">&#8211;</span><span style="color: rgb(0, 0, 0);">) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">a[j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">]; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[temp]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">temp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br />
&nbsp;&nbsp;&nbsp;&nbsp;} <br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">; <br />
} </p>
<p></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> print(){ <br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(128, 0, 128);">10</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">a[i]</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl; <br />
}</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jguoer.com/blog/index.php/archives/957/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>强题看深度优先搜索DFS</title>
		<link>http://www.jguoer.com/blog/index.php/archives/139</link>
		<comments>http://www.jguoer.com/blog/index.php/archives/139#comments</comments>
		<pubDate>Wed, 17 Feb 2010 05:14:00 +0000</pubDate>
		<dc:creator>诡异的西红柿</dc:creator>
				<category><![CDATA[数据结构]]></category>
		<category><![CDATA[趣题]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[搜索]]></category>
		<category><![CDATA[深度优先]]></category>

		<guid isPermaLink="false">/post/2010/02/17/DFS-Code.aspx</guid>
		<description><![CDATA[昨天晚上看了一个题目，这个题目很强，一开始在做的时候，没有考虑那么多，想来想去还是没有什么头绪，后来看到一个哥们写的题目发现特别简单，但是特别难懂，后来查了一下相关资料，哇，确实是一个强题，可能一般人来看这个题目的话，代码可能会写很多，而且效率也不算很高，但是这个标准解答还是很不错的。题目如下。

结果为0的序列：

考虑由1到n(n&#60;=9)按递增顺序排成的序列1234...n,在他们之间加入加号，减号或者什么也不加，分别使它们做加法，减法或者将数字合并。然后求出结果，看看是否为0。

写一个程序，找出所有长度为n的结果为0的序列,返回序列的个数。

函数接口：int ResultIsZero(int n);

其中n表示序列的长度，int表示返回的序列的个数。

举例：

n = 8，返回10。

序列分别为：

1+2+3+4-5-6-7+8 = 0;

1+2+3-4+5-6+7-8 = 0;

1+2-3+4+5+6-7-8 = 0;

1+2-3-4-5-6+7+8 = 0;

1-2+3-4-5+6-7+8 = 0;

1-2-3+4+5-6-7+8 = 0;

1-2-3+4-5+6+7-8 = 0;

1+23-45+6+7+8 = 0;

12-34-56+78 = 0;

1-23-4+5+6+7+8 = 0;

如果你看明白了题目，你可以开始做这个题目了，如果你看明白了但是想了半天和我一样不会做的话，那就需要先来了解一下深度优先搜索DFS了。

了解<a href="http://baike.baidu.com/view/288277.htm" target="_blank"><strong>百度百科</strong></a>，还有一个<a href="http://www.comp.nus.edu.sg/~xujia/mirror/algorithm.myrice.com/algorithm/commonalg/graph/traversal/dfs.htm" target="_blank"><strong>详细资料</strong></a>，关于图论的，关于<a href="http://learn.akae.cn/media/ch12s03.html" target="_blank"><strong>迷宫的深度搜索算法</strong></a>。下面我给出一个迷宫的算法，迷宫如下图所示。
<p style="text-align: center;"><img src="/blog/wp-content/uploads/2010/2/migong.png" alt="" /></p>
迷宫的代码如下：
<div class="code">
<div>
<img id="Highlighter_0_836_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_0_836_Closed_Text').style.display='none'; document.getElementById('Highlighter_0_836_Open_Image').style.display='inline'; document.getElementById('Highlighter_0_836_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_0_836_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_0_836_Open_Text').style.display='none'; document.getElementById('Highlighter_0_836_Closed_Image').style.display='inline'; document.getElementById('Highlighter_0_836_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><span id="Highlighter_0_836_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Code</span><span id="Highlighter_0_836_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Code</span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />#include </span><span style="color: #000000;">&#60;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&#62;</span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> std::cout;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> std::endl;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n[</span><span style="color: #800080;">5</span><span style="color: #000000;">][</span><span style="color: #800080;">5</span><span style="color: #000000;">];

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> init()

<img id="Highlighter_94_486_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_94_486_Open_Text').style.display='none'; document.getElementById('Highlighter_94_486_Closed_Image').style.display='inline'; document.getElementById('Highlighter_94_486_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_94_486_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_94_486_Closed_Text').style.display='none'; document.getElementById('Highlighter_94_486_Open_Image').style.display='inline'; document.getElementById('Highlighter_94_486_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_94_486_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_94_486_Open_Text"><span style="color: #000000;">{

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">0</span><span style="color: #000000;">][</span><span style="color: #800080;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">0</span><span style="color: #000000;">][</span><span style="color: #800080;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">0</span><span style="color: #000000;">][</span><span style="color: #800080;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">0</span><span style="color: #000000;">][</span><span style="color: #800080;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">0</span><span style="color: #000000;">][</span><span style="color: #800080;">4</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">1</span><span style="color: #000000;">][</span><span style="color: #800080;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">1</span><span style="color: #000000;">][</span><span style="color: #800080;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">1</span><span style="color: #000000;">][</span><span style="color: #800080;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">1</span><span style="color: #000000;">][</span><span style="color: #800080;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">1</span><span style="color: #000000;">][</span><span style="color: #800080;">4</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">2</span><span style="color: #000000;">][</span><span style="color: #800080;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">2</span><span style="color: #000000;">][</span><span style="color: #800080;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">2</span><span style="color: #000000;">][</span><span style="color: #800080;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">2</span><span style="color: #000000;">][</span><span style="color: #800080;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">2</span><span style="color: #000000;">][</span><span style="color: #800080;">4</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">3</span><span style="color: #000000;">][</span><span style="color: #800080;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">3</span><span style="color: #000000;">][</span><span style="color: #800080;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">3</span><span style="color: #000000;">][</span><span style="color: #800080;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">3</span><span style="color: #000000;">][</span><span style="color: #800080;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">3</span><span style="color: #000000;">][</span><span style="color: #800080;">4</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">4</span><span style="color: #000000;">][</span><span style="color: #800080;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">4</span><span style="color: #000000;">][</span><span style="color: #800080;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">4</span><span style="color: #000000;">][</span><span style="color: #800080;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">4</span><span style="color: #000000;">][</span><span style="color: #800080;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">4</span><span style="color: #000000;">][</span><span style="color: #800080;">4</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> find(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> x,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> y)

<img id="Highlighter_510_736_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_510_736_Open_Text').style.display='none'; document.getElementById('Highlighter_510_736_Closed_Image').style.display='inline'; document.getElementById('Highlighter_510_736_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_510_736_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_510_736_Closed_Text').style.display='none'; document.getElementById('Highlighter_510_736_Open_Image').style.display='inline'; document.getElementById('Highlighter_510_736_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_510_736_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_510_736_Open_Text"><span style="color: #000000;">{

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">x</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">y</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">endl;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(x</span><span style="color: #000000;">==</span><span style="color: #800080;">4</span><span style="color: #000000;">&#38;&#38;</span><span style="color: #000000;">y</span><span style="color: #000000;">==</span><span style="color: #800080;">4</span><span style="color: #000000;">)

<img id="Highlighter_563_608_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_563_608_Open_Text').style.display='none'; document.getElementById('Highlighter_563_608_Closed_Image').style.display='inline'; document.getElementById('Highlighter_563_608_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_563_608_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_563_608_Closed_Text').style.display='none'; document.getElementById('Highlighter_563_608_Open_Image').style.display='inline'; document.getElementById('Highlighter_563_608_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_563_608_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_563_608_Open_Text"><span style="color: #000000;">{

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">endl;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n[x][y]</span><span style="color: #000000;">==</span><span style="color: #800080;">0</span><span style="color: #000000;">&#124;&#124;</span><span style="color: #000000;">x</span><span style="color: #000000;">&#62;</span><span style="color: #800080;">4</span><span style="color: #000000;">&#124;&#124;</span><span style="color: #000000;">y</span><span style="color: #000000;">&#62;</span><span style="color: #800080;">4</span><span style="color: #000000;">&#124;&#124;</span><span style="color: #000000;">x</span><span style="color: #000000;">&#60;</span><span style="color: #800080;">0</span><span style="color: #000000;">&#124;&#124;</span><span style="color: #000000;">y</span><span style="color: #000000;">&#60;</span><span style="color: #800080;">0</span><span style="color: #000000;">)

<img id="Highlighter_657_682_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_657_682_Open_Text').style.display='none'; document.getElementById('Highlighter_657_682_Closed_Image').style.display='inline'; document.getElementById('Highlighter_657_682_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_657_682_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_657_682_Closed_Text').style.display='none'; document.getElementById('Highlighter_657_682_Open_Image').style.display='inline'; document.getElementById('Highlighter_657_682_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_657_682_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_657_682_Open_Text"><span style="color: #000000;">{

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[x][y]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> find(x,y</span><span style="color: #000000;">+</span><span style="color: #800080;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #000000;">find(x</span><span style="color: #000000;">+</span><span style="color: #800080;">1</span><span style="color: #000000;">,y);

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">)

<img id="Highlighter_753_824_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_753_824_Open_Text').style.display='none'; document.getElementById('Highlighter_753_824_Closed_Image').style.display='inline'; document.getElementById('Highlighter_753_824_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_753_824_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_753_824_Closed_Text').style.display='none'; document.getElementById('Highlighter_753_824_Open_Image').style.display='inline'; document.getElementById('Highlighter_753_824_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_753_824_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_753_824_Open_Text"><span style="color: #000000;">{

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> init();

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> r </span><span style="color: #000000;">=</span><span style="color: #000000;"> find(</span><span style="color: #800080;">0</span><span style="color: #000000;">,</span><span style="color: #800080;">0</span><span style="color: #000000;">);

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">r</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">endl;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span>

</div>
</div>
<strong>我们只有了解了迷宫的深度优先的搜索的递归算法，才能再来回头看我们前面说的题目</strong>，那个找结果为0的数字的表达式集合，我们同样可以用深度优先搜索计算，代码如下所示。
<div class="code">
<div>
<img id="Highlighter_0_662_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_0_662_Closed_Text').style.display='none'; document.getElementById('Highlighter_0_662_Open_Image').style.display='inline'; document.getElementById('Highlighter_0_662_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_0_662_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_0_662_Open_Text').style.display='none'; document.getElementById('Highlighter_0_662_Closed_Image').style.display='inline'; document.getElementById('Highlighter_0_662_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><span id="Highlighter_0_662_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Code</span><span id="Highlighter_0_662_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Code</span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />#include </span><span style="color: #000000;">&#60;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&#62;</span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> std::cout;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> std::endl;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> N;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ResultIsZeroDFS(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> total, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> last)

<img id="Highlighter_139_512_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_139_512_Open_Text').style.display='none'; document.getElementById('Highlighter_139_512_Closed_Image').style.display='inline'; document.getElementById('Highlighter_139_512_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_139_512_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_139_512_Closed_Text').style.display='none'; document.getElementById('Highlighter_139_512_Open_Image').style.display='inline'; document.getElementById('Highlighter_139_512_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_139_512_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_139_512_Open_Text"><span style="color: #000000;">{

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">const</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> current </span><span style="color: #000000;">=</span><span style="color: #000000;"> n</span><span style="color: #000000;">+</span><span style="color: #800080;">1</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> sub </span><span style="color: #000000;">=</span><span style="color: #000000;"> total</span><span style="color: #000000;">+</span><span style="color: #000000;">last;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">( n </span><span style="color: #000000;">==</span><span style="color: #000000;"> N )

<img id="Highlighter_217_258_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_217_258_Open_Text').style.display='none'; document.getElementById('Highlighter_217_258_Closed_Image').style.display='inline'; document.getElementById('Highlighter_217_258_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_217_258_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_217_258_Closed_Text').style.display='none'; document.getElementById('Highlighter_217_258_Open_Image').style.display='inline'; document.getElementById('Highlighter_217_258_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_217_258_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_217_258_Open_Text"><span style="color: #000000;">{

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> (total</span><span style="color: #000000;">+</span><span style="color: #000000;">last </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">);

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;">

<img id="Highlighter_272_510_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_272_510_Open_Text').style.display='none'; document.getElementById('Highlighter_272_510_Closed_Image').style.display='inline'; document.getElementById('Highlighter_272_510_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_272_510_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_272_510_Closed_Text').style.display='none'; document.getElementById('Highlighter_272_510_Open_Image').style.display='inline'; document.getElementById('Highlighter_272_510_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_272_510_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_272_510_Open_Text"><span style="color: #000000;">{

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> result </span><span style="color: #000000;">=</span><span style="color: #000000;"> ResultIsZeroDFS(current, total, last</span><span style="color: #000000;">*</span><span style="color: #800080;">10</span><span style="color: #000000;">+</span><span style="color: #000000;">current)</span><span style="color: #000000;">+</span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> ResultIsZeroDFS(current, total</span><span style="color: #000000;">+</span><span style="color: #000000;">last, current)</span><span style="color: #000000;">+</span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> ResultIsZeroDFS(current, total</span><span style="color: #000000;">-</span><span style="color: #000000;">last, current);

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ResultIsZero(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n)

<img id="Highlighter_538_589_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_538_589_Open_Text').style.display='none'; document.getElementById('Highlighter_538_589_Closed_Image').style.display='inline'; document.getElementById('Highlighter_538_589_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_538_589_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_538_589_Closed_Text').style.display='none'; document.getElementById('Highlighter_538_589_Open_Image').style.display='inline'; document.getElementById('Highlighter_538_589_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_538_589_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_538_589_Open_Text"><span style="color: #000000;">{

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> N </span><span style="color: #000000;">=</span><span style="color: #000000;"> n;

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ResultIsZeroDFS(</span><span style="color: #800080;">1</span><span style="color: #000000;">, </span><span style="color: #800080;">0</span><span style="color: #000000;">, </span><span style="color: #800080;">1</span><span style="color: #000000;">);

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">)

<img id="Highlighter_606_650_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_606_650_Open_Text').style.display='none'; document.getElementById('Highlighter_606_650_Closed_Image').style.display='inline'; document.getElementById('Highlighter_606_650_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_606_650_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_606_650_Closed_Text').style.display='none'; document.getElementById('Highlighter_606_650_Open_Image').style.display='inline'; document.getElementById('Highlighter_606_650_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_606_650_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_606_650_Open_Text"><span style="color: #000000;">{

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">ResultIsZero(</span><span style="color: #800080;">8</span><span style="color: #000000;">);

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">

<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />

<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span>

</div>
</div>
这个答案是不是很强，其实其中还有很多值得我们去学习。。]]></description>
			<content:encoded><![CDATA[<p>昨天晚上看了一个题目，这个题目很强，一开始在做的时候，没有考虑那么多，想来想去还是没有什么头绪，后来看到一个哥们写的题目发现特别简单，但是特别难懂，后来查了一下相关资料，哇，确实是一个强题，可能一般人来看这个题目的话，代码可能会写很多，而且效率也不算很高，但是这个标准解答还是很不错的。题目如下。</p>
<p>结果为0的序列：</p>
<p>考虑由1到n(n&lt;=9)按递增顺序排成的序列1234&#8230;n,在他们之间加入加号，减号或者什么也不加，分别使它们做加法，减法或者将数字合并。然后求出结果，看看是否为0。</p>
<p>写一个程序，找出所有长度为n的结果为0的序列,返回序列的个数。</p>
<p>函数接口：int ResultIsZero(int n);</p>
<p>其中n表示序列的长度，int表示返回的序列的个数。</p>
<p>举例：</p>
<p>n = 8，返回10。</p>
<p>序列分别为：</p>
<p>1+2+3+4-5-6-7+8 = 0;</p>
<p>1+2+3-4+5-6+7-8 = 0;</p>
<p>1+2-3+4+5+6-7-8 = 0;</p>
<p>1+2-3-4-5-6+7+8 = 0;</p>
<p>1-2+3-4-5+6-7+8 = 0;</p>
<p>1-2-3+4+5-6-7+8 = 0;</p>
<p>1-2-3+4-5+6+7-8 = 0;</p>
<p>1+23-45+6+7+8 = 0;</p>
<p>12-34-56+78 = 0;</p>
<p>1-23-4+5+6+7+8 = 0;</p>
<p>如果你看明白了题目，你可以开始做这个题目了，如果你看明白了但是想了半天和我一样不会做的话，那就需要先来了解一下深度优先搜索DFS了。</p>
<p>了解<a href="http://baike.baidu.com/view/288277.htm" target="_blank"><strong>百度百科</strong></a>，还有一个<a href="http://www.comp.nus.edu.sg/~xujia/mirror/algorithm.myrice.com/algorithm/commonalg/graph/traversal/dfs.htm" target="_blank"><strong>详细资料</strong></a>，关于图论的，关于<a href="http://learn.akae.cn/media/ch12s03.html" target="_blank"><strong>迷宫的深度搜索算法</strong></a>。下面我给出一个迷宫的算法，迷宫如下图所示。</p>
<p style="text-align: center;"><img src="/blog/wp-content/uploads/2010/2/migong.png" alt="" /></p>
<p>迷宫的代码如下：</p>
<div class="code">
<div>
<img id="Highlighter_0_836_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_0_836_Closed_Text').style.display='none'; document.getElementById('Highlighter_0_836_Open_Image').style.display='inline'; document.getElementById('Highlighter_0_836_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_0_836_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_0_836_Open_Text').style.display='none'; document.getElementById('Highlighter_0_836_Closed_Image').style.display='inline'; document.getElementById('Highlighter_0_836_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><span id="Highlighter_0_836_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Code</span><span id="Highlighter_0_836_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Code</span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />#include </span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> std::cout;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> std::endl;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n[</span><span style="color: #800080;">5</span><span style="color: #000000;">][</span><span style="color: #800080;">5</span><span style="color: #000000;">];</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> init()</p>
<p><img id="Highlighter_94_486_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_94_486_Open_Text').style.display='none'; document.getElementById('Highlighter_94_486_Closed_Image').style.display='inline'; document.getElementById('Highlighter_94_486_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_94_486_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_94_486_Closed_Text').style.display='none'; document.getElementById('Highlighter_94_486_Open_Image').style.display='inline'; document.getElementById('Highlighter_94_486_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_94_486_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_94_486_Open_Text"><span style="color: #000000;">{</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">0</span><span style="color: #000000;">][</span><span style="color: #800080;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">0</span><span style="color: #000000;">][</span><span style="color: #800080;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">0</span><span style="color: #000000;">][</span><span style="color: #800080;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">0</span><span style="color: #000000;">][</span><span style="color: #800080;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">0</span><span style="color: #000000;">][</span><span style="color: #800080;">4</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">1</span><span style="color: #000000;">][</span><span style="color: #800080;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">1</span><span style="color: #000000;">][</span><span style="color: #800080;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">1</span><span style="color: #000000;">][</span><span style="color: #800080;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">1</span><span style="color: #000000;">][</span><span style="color: #800080;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">1</span><span style="color: #000000;">][</span><span style="color: #800080;">4</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">2</span><span style="color: #000000;">][</span><span style="color: #800080;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">2</span><span style="color: #000000;">][</span><span style="color: #800080;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">2</span><span style="color: #000000;">][</span><span style="color: #800080;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">2</span><span style="color: #000000;">][</span><span style="color: #800080;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">2</span><span style="color: #000000;">][</span><span style="color: #800080;">4</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">3</span><span style="color: #000000;">][</span><span style="color: #800080;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">3</span><span style="color: #000000;">][</span><span style="color: #800080;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">3</span><span style="color: #000000;">][</span><span style="color: #800080;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">3</span><span style="color: #000000;">][</span><span style="color: #800080;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">3</span><span style="color: #000000;">][</span><span style="color: #800080;">4</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">4</span><span style="color: #000000;">][</span><span style="color: #800080;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">4</span><span style="color: #000000;">][</span><span style="color: #800080;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">4</span><span style="color: #000000;">][</span><span style="color: #800080;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">4</span><span style="color: #000000;">][</span><span style="color: #800080;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[</span><span style="color: #800080;">4</span><span style="color: #000000;">][</span><span style="color: #800080;">4</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> find(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> x,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> y)</p>
<p><img id="Highlighter_510_736_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_510_736_Open_Text').style.display='none'; document.getElementById('Highlighter_510_736_Closed_Image').style.display='inline'; document.getElementById('Highlighter_510_736_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_510_736_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_510_736_Closed_Text').style.display='none'; document.getElementById('Highlighter_510_736_Open_Image').style.display='inline'; document.getElementById('Highlighter_510_736_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_510_736_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_510_736_Open_Text"><span style="color: #000000;">{</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">x</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #800000;">&#8220;</span><span style="color: #800000;">\t</span><span style="color: #800000;">&#8220;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">y</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(x</span><span style="color: #000000;">==</span><span style="color: #800080;">4</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">y</span><span style="color: #000000;">==</span><span style="color: #800080;">4</span><span style="color: #000000;">)</p>
<p><img id="Highlighter_563_608_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_563_608_Open_Text').style.display='none'; document.getElementById('Highlighter_563_608_Closed_Image').style.display='inline'; document.getElementById('Highlighter_563_608_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_563_608_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_563_608_Closed_Text').style.display='none'; document.getElementById('Highlighter_563_608_Open_Image').style.display='inline'; document.getElementById('Highlighter_563_608_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_563_608_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_563_608_Open_Text"><span style="color: #000000;">{</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n[x][y]</span><span style="color: #000000;">==</span><span style="color: #800080;">0</span><span style="color: #000000;">||</span><span style="color: #000000;">x</span><span style="color: #000000;">&gt;</span><span style="color: #800080;">4</span><span style="color: #000000;">||</span><span style="color: #000000;">y</span><span style="color: #000000;">&gt;</span><span style="color: #800080;">4</span><span style="color: #000000;">||</span><span style="color: #000000;">x</span><span style="color: #000000;">&lt;</span><span style="color: #800080;">0</span><span style="color: #000000;">||</span><span style="color: #000000;">y</span><span style="color: #000000;">&lt;</span><span style="color: #800080;">0</span><span style="color: #000000;">)</p>
<p><img id="Highlighter_657_682_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_657_682_Open_Text').style.display='none'; document.getElementById('Highlighter_657_682_Closed_Image').style.display='inline'; document.getElementById('Highlighter_657_682_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_657_682_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_657_682_Closed_Text').style.display='none'; document.getElementById('Highlighter_657_682_Open_Image').style.display='inline'; document.getElementById('Highlighter_657_682_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_657_682_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_657_682_Open_Text"><span style="color: #000000;">{</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> n[x][y]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> find(x,y</span><span style="color: #000000;">+</span><span style="color: #800080;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #000000;">find(x</span><span style="color: #000000;">+</span><span style="color: #800080;">1</span><span style="color: #000000;">,y);</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">)</p>
<p><img id="Highlighter_753_824_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_753_824_Open_Text').style.display='none'; document.getElementById('Highlighter_753_824_Closed_Image').style.display='inline'; document.getElementById('Highlighter_753_824_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_753_824_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_753_824_Closed_Text').style.display='none'; document.getElementById('Highlighter_753_824_Open_Image').style.display='inline'; document.getElementById('Highlighter_753_824_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_753_824_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_753_824_Open_Text"><span style="color: #000000;">{</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> init();</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> r </span><span style="color: #000000;">=</span><span style="color: #000000;"> find(</span><span style="color: #800080;">0</span><span style="color: #000000;">,</span><span style="color: #800080;">0</span><span style="color: #000000;">);</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">r</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span></p>
</div>
</div>
<p><strong>我们只有了解了迷宫的深度优先的搜索的递归算法，才能再来回头看我们前面说的题目</strong>，那个找结果为0的数字的表达式集合，我们同样可以用深度优先搜索计算，代码如下所示。</p>
<div class="code">
<div>
<img id="Highlighter_0_662_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_0_662_Closed_Text').style.display='none'; document.getElementById('Highlighter_0_662_Open_Image').style.display='inline'; document.getElementById('Highlighter_0_662_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_0_662_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_0_662_Open_Text').style.display='none'; document.getElementById('Highlighter_0_662_Closed_Image').style.display='inline'; document.getElementById('Highlighter_0_662_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><span id="Highlighter_0_662_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Code</span><span id="Highlighter_0_662_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Code</span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />#include </span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> std::cout;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> std::endl;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> N;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ResultIsZeroDFS(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> total, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> last)</p>
<p><img id="Highlighter_139_512_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_139_512_Open_Text').style.display='none'; document.getElementById('Highlighter_139_512_Closed_Image').style.display='inline'; document.getElementById('Highlighter_139_512_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_139_512_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_139_512_Closed_Text').style.display='none'; document.getElementById('Highlighter_139_512_Open_Image').style.display='inline'; document.getElementById('Highlighter_139_512_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_139_512_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_139_512_Open_Text"><span style="color: #000000;">{</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">const</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> current </span><span style="color: #000000;">=</span><span style="color: #000000;"> n</span><span style="color: #000000;">+</span><span style="color: #800080;">1</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> sub </span><span style="color: #000000;">=</span><span style="color: #000000;"> total</span><span style="color: #000000;">+</span><span style="color: #000000;">last;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">( n </span><span style="color: #000000;">==</span><span style="color: #000000;"> N )</p>
<p><img id="Highlighter_217_258_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_217_258_Open_Text').style.display='none'; document.getElementById('Highlighter_217_258_Closed_Image').style.display='inline'; document.getElementById('Highlighter_217_258_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_217_258_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_217_258_Closed_Text').style.display='none'; document.getElementById('Highlighter_217_258_Open_Image').style.display='inline'; document.getElementById('Highlighter_217_258_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_217_258_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_217_258_Open_Text"><span style="color: #000000;">{</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> (total</span><span style="color: #000000;">+</span><span style="color: #000000;">last </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">);</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"></p>
<p><img id="Highlighter_272_510_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_272_510_Open_Text').style.display='none'; document.getElementById('Highlighter_272_510_Closed_Image').style.display='inline'; document.getElementById('Highlighter_272_510_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_272_510_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_272_510_Closed_Text').style.display='none'; document.getElementById('Highlighter_272_510_Open_Image').style.display='inline'; document.getElementById('Highlighter_272_510_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_272_510_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_272_510_Open_Text"><span style="color: #000000;">{</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> result </span><span style="color: #000000;">=</span><span style="color: #000000;"> ResultIsZeroDFS(current, total, last</span><span style="color: #000000;">*</span><span style="color: #800080;">10</span><span style="color: #000000;">+</span><span style="color: #000000;">current)</span><span style="color: #000000;">+</span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> ResultIsZeroDFS(current, total</span><span style="color: #000000;">+</span><span style="color: #000000;">last, current)</span><span style="color: #000000;">+</span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> ResultIsZeroDFS(current, total</span><span style="color: #000000;">-</span><span style="color: #000000;">last, current);</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ResultIsZero(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n)</p>
<p><img id="Highlighter_538_589_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_538_589_Open_Text').style.display='none'; document.getElementById('Highlighter_538_589_Closed_Image').style.display='inline'; document.getElementById('Highlighter_538_589_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_538_589_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_538_589_Closed_Text').style.display='none'; document.getElementById('Highlighter_538_589_Open_Image').style.display='inline'; document.getElementById('Highlighter_538_589_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_538_589_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_538_589_Open_Text"><span style="color: #000000;">{</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> N </span><span style="color: #000000;">=</span><span style="color: #000000;"> n;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ResultIsZeroDFS(</span><span style="color: #800080;">1</span><span style="color: #000000;">, </span><span style="color: #800080;">0</span><span style="color: #000000;">, </span><span style="color: #800080;">1</span><span style="color: #000000;">);</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">)</p>
<p><img id="Highlighter_606_650_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_606_650_Open_Text').style.display='none'; document.getElementById('Highlighter_606_650_Closed_Image').style.display='inline'; document.getElementById('Highlighter_606_650_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_606_650_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_606_650_Closed_Text').style.display='none'; document.getElementById('Highlighter_606_650_Open_Image').style.display='inline'; document.getElementById('Highlighter_606_650_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_606_650_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_606_650_Open_Text"><span style="color: #000000;">{</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">ResultIsZero(</span><span style="color: #800080;">8</span><span style="color: #000000;">);</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;</p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></p>
<p><img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span></p>
</div>
</div>
<p>这个答案是不是很强，其实其中还有很多值得我们去学习。。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jguoer.com/blog/index.php/archives/139/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>一道非常简单的题</title>
		<link>http://www.jguoer.com/blog/index.php/archives/142</link>
		<comments>http://www.jguoer.com/blog/index.php/archives/142#comments</comments>
		<pubDate>Tue, 16 Feb 2010 06:10:00 +0000</pubDate>
		<dc:creator>诡异的西红柿</dc:creator>
				<category><![CDATA[数据结构]]></category>
		<category><![CDATA[趣题]]></category>

		<guid isPermaLink="false">/post/2010/02/16/A-Easy-Problem.aspx</guid>
		<description><![CDATA[今天一个朋友发来一道题，初步看上去好像蛮有挑战的，但是仔细想了一下之后其实好像没那么难，好像也没什么特别的陷阱，琢磨了一下，貌似也真的没什么好检查的，所以觉得也不是很难，但是朋友发的，所以就贴上来告知一下答案了。。哎，其实也没什么好些的，放假的时候无聊的朋友也可以练一下小手。。

已知f[]和g[]两个数组，大小分别为m和n，其中的元素都已经从小到大排列，且每个数组内的元素都各不相同。

例：f[5] = {1,3,4,7,9}, g[4] = {3,5,7,8};     因为只有2组元素相同，分别是f[1]与g[0] 和 f[3]与g[2]，所以函数Count的返回值应为2。

C++代码如下：
<div class="code">
<div><img id="Highlighter_0_1591_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_0_1591_Closed_Text').style.display='none'; document.getElementById('Highlighter_0_1591_Open_Image').style.display='inline'; document.getElementById('Highlighter_0_1591_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_0_1591_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_0_1591_Open_Text').style.display='none'; document.getElementById('Highlighter_0_1591_Closed_Image').style.display='inline'; document.getElementById('Highlighter_0_1591_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><span id="Highlighter_0_1591_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Code</span><span id="Highlighter_0_1591_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Code</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">虽然没写思路，不过看上去非常简单，也不用我多说了＝。＝
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">题目是两个数组长度不一样，不过无所谓，这个不是关键</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;">#include </span><span style="color: #000000;">&#60;</span><span style="color: #000000;">stdlib.h</span><span style="color: #000000;">&#62;</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />#include </span><span style="color: #000000;">&#60;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&#62;</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;"> MAX 10</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> </span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> std;
<img id="Highlighter_149_159_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_149_159_Open_Text').style.display='none'; document.getElementById('Highlighter_149_159_Closed_Image').style.display='inline'; document.getElementById('Highlighter_149_159_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_149_159_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_149_159_Closed_Text').style.display='none'; document.getElementById('Highlighter_149_159_Open_Image').style.display='inline'; document.getElementById('Highlighter_149_159_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_149_159_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">/**/</span><span id="Highlighter_149_159_Open_Text"><span style="color: #008000;">/*</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> *
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #008000;">*/</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> num1[MAX];
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> num2[MAX];
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> result[MAX];
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> resultCount;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> init();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> find();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> print();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img id="Highlighter_295_370_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_295_370_Open_Text').style.display='none'; document.getElementById('Highlighter_295_370_Closed_Image').style.display='inline'; document.getElementById('Highlighter_295_370_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_295_370_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_295_370_Closed_Text').style.display='none'; document.getElementById('Highlighter_295_370_Open_Image').style.display='inline'; document.getElementById('Highlighter_295_370_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> argc, </span><span style="color: #0000ff;">char</span><span style="color: #000000;">**</span><span style="color: #000000;"> argv) </span><span id="Highlighter_295_370_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_295_370_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> resultCount </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> init();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> find();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> print();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> init()
<img id="Highlighter_383_835_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_383_835_Open_Text').style.display='none'; document.getElementById('Highlighter_383_835_Closed_Image').style.display='inline'; document.getElementById('Highlighter_383_835_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_383_835_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_383_835_Closed_Text').style.display='none'; document.getElementById('Highlighter_383_835_Open_Image').style.display='inline'; document.getElementById('Highlighter_383_835_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_383_835_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_383_835_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">0</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">3</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">2</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">4</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">3</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">5</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">4</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">7</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">5</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">9</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">6</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">11</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">7</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">21</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">8</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">24</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">9</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">30</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">0</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">5</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">2</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">7</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">3</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">8</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">4</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">10</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">5</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">17</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">6</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">18</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">7</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">23</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">8</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">27</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">9</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">30</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> count</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">; count</span><span style="color: #000000;">&#60;</span><span style="color: #000000;">MAX; count</span><span style="color: #000000;">++</span><span style="color: #000000;">)
<img id="Highlighter_786_818_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_786_818_Open_Text').style.display='none'; document.getElementById('Highlighter_786_818_Closed_Image').style.display='inline'; document.getElementById('Highlighter_786_818_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_786_818_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_786_818_Closed_Text').style.display='none'; document.getElementById('Highlighter_786_818_Open_Image').style.display='inline'; document.getElementById('Highlighter_786_818_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_786_818_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_786_818_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> result[count]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> find()
<img id="Highlighter_848_1410_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_848_1410_Open_Text').style.display='none'; document.getElementById('Highlighter_848_1410_Closed_Image').style.display='inline'; document.getElementById('Highlighter_848_1410_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_848_1410_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_848_1410_Closed_Text').style.display='none'; document.getElementById('Highlighter_848_1410_Open_Image').style.display='inline'; document.getElementById('Highlighter_848_1410_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_848_1410_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_848_1410_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> num1Index </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> num2Index </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> resultIndex </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(num1Index </span><span style="color: #000000;">&#60;</span><span style="color: #000000;"> MAX </span><span style="color: #000000;">&#38;&#38;</span><span style="color: #000000;"> num2Index </span><span style="color: #000000;">&#60;</span><span style="color: #000000;"> MAX)
<img id="Highlighter_972_1393_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_972_1393_Open_Text').style.display='none'; document.getElementById('Highlighter_972_1393_Closed_Image').style.display='inline'; document.getElementById('Highlighter_972_1393_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_972_1393_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_972_1393_Closed_Text').style.display='none'; document.getElementById('Highlighter_972_1393_Open_Image').style.display='inline'; document.getElementById('Highlighter_972_1393_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_972_1393_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_972_1393_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(num1[num1Index] </span><span style="color: #000000;">&#60;</span><span style="color: #000000;"> num2[num2Index])
<img id="Highlighter_1028_1064_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1028_1064_Open_Text').style.display='none'; document.getElementById('Highlighter_1028_1064_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1028_1064_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1028_1064_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1028_1064_Closed_Text').style.display='none'; document.getElementById('Highlighter_1028_1064_Open_Image').style.display='inline'; document.getElementById('Highlighter_1028_1064_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1028_1064_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1028_1064_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1Index</span><span style="color: #000000;">++</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(num1[num1Index] </span><span style="color: #000000;">&#62;</span><span style="color: #000000;"> num2[num2Index])
<img id="Highlighter_1124_1160_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1124_1160_Open_Text').style.display='none'; document.getElementById('Highlighter_1124_1160_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1124_1160_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1124_1160_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1124_1160_Closed_Text').style.display='none'; document.getElementById('Highlighter_1124_1160_Open_Image').style.display='inline'; document.getElementById('Highlighter_1124_1160_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1124_1160_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1124_1160_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2Index</span><span style="color: #000000;">++</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(num1[num1Index] </span><span style="color: #000000;">==</span><span style="color: #000000;"> num2[num2Index])
<img id="Highlighter_1221_1387_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1221_1387_Open_Text').style.display='none'; document.getElementById('Highlighter_1221_1387_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1221_1387_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1221_1387_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1221_1387_Closed_Text').style.display='none'; document.getElementById('Highlighter_1221_1387_Open_Image').style.display='inline'; document.getElementById('Highlighter_1221_1387_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1221_1387_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1221_1387_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> result[resultIndex] </span><span style="color: #000000;">=</span><span style="color: #000000;"> num1[num1Index];
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> resultIndex</span><span style="color: #000000;">++</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1Index</span><span style="color: #000000;">++</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2Index</span><span style="color: #000000;">++</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> resultCount</span><span style="color: #000000;">++</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> print()
<img id="Highlighter_1424_1579_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1424_1579_Open_Text').style.display='none'; document.getElementById('Highlighter_1424_1579_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1424_1579_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1424_1579_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1424_1579_Closed_Text').style.display='none'; document.getElementById('Highlighter_1424_1579_Open_Image').style.display='inline'; document.getElementById('Highlighter_1424_1579_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_1424_1579_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1424_1579_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> count</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">; count</span><span style="color: #000000;">&#60;</span><span style="color: #000000;">MAX; count</span><span style="color: #000000;">++</span><span style="color: #000000;">)
<img id="Highlighter_1476_1518_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1476_1518_Open_Text').style.display='none'; document.getElementById('Highlighter_1476_1518_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1476_1518_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1476_1518_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1476_1518_Closed_Text').style.display='none'; document.getElementById('Highlighter_1476_1518_Open_Image').style.display='inline'; document.getElementById('Highlighter_1476_1518_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1476_1518_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1476_1518_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">result[count]</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">endl;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #800000;">"</span><span style="color: #800000;">Result is </span><span style="color: #800000;">"</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">resultCount</span><span style="color: #000000;">&#60;&#60;</span><span style="color: #000000;">endl;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span></div>
</div>]]></description>
			<content:encoded><![CDATA[<p>今天一个朋友发来一道题，初步看上去好像蛮有挑战的，但是仔细想了一下之后其实好像没那么难，好像也没什么特别的陷阱，琢磨了一下，貌似也真的没什么好检查的，所以觉得也不是很难，但是朋友发的，所以就贴上来告知一下答案了。。哎，其实也没什么好些的，放假的时候无聊的朋友也可以练一下小手。。</p>
<p>已知f[]和g[]两个数组，大小分别为m和n，其中的元素都已经从小到大排列，且每个数组内的元素都各不相同。</p>
<p>例：f[5] = {1,3,4,7,9}, g[4] = {3,5,7,8};     因为只有2组元素相同，分别是f[1]与g[0] 和 f[3]与g[2]，所以函数Count的返回值应为2。</p>
<p>C++代码如下：</p>
<div class="code">
<div><img id="Highlighter_0_1591_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_0_1591_Closed_Text').style.display='none'; document.getElementById('Highlighter_0_1591_Open_Image').style.display='inline'; document.getElementById('Highlighter_0_1591_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_0_1591_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_0_1591_Open_Text').style.display='none'; document.getElementById('Highlighter_0_1591_Closed_Image').style.display='inline'; document.getElementById('Highlighter_0_1591_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><span id="Highlighter_0_1591_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Code</span><span id="Highlighter_0_1591_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Code</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">虽然没写思路，不过看上去非常简单，也不用我多说了＝。＝<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">题目是两个数组长度不一样，不过无所谓，这个不是关键</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;">#include </span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdlib.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />#include </span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;"> MAX 10</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> </span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> std;<br />
<img id="Highlighter_149_159_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_149_159_Open_Text').style.display='none'; document.getElementById('Highlighter_149_159_Closed_Image').style.display='inline'; document.getElementById('Highlighter_149_159_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_149_159_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_149_159_Closed_Text').style.display='none'; document.getElementById('Highlighter_149_159_Open_Image').style.display='inline'; document.getElementById('Highlighter_149_159_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_149_159_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">/**/</span><span id="Highlighter_149_159_Open_Text"><span style="color: #008000;">/*</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> *<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #008000;">*/</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> num1[MAX];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> num2[MAX];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> result[MAX];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> resultCount;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> init();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> find();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> print();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img id="Highlighter_295_370_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_295_370_Open_Text').style.display='none'; document.getElementById('Highlighter_295_370_Closed_Image').style.display='inline'; document.getElementById('Highlighter_295_370_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_295_370_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_295_370_Closed_Text').style.display='none'; document.getElementById('Highlighter_295_370_Open_Image').style.display='inline'; document.getElementById('Highlighter_295_370_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> argc, </span><span style="color: #0000ff;">char</span><span style="color: #000000;">**</span><span style="color: #000000;"> argv) </span><span id="Highlighter_295_370_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_295_370_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> resultCount </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> init();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> find();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> print();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> init()<br />
<img id="Highlighter_383_835_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_383_835_Open_Text').style.display='none'; document.getElementById('Highlighter_383_835_Closed_Image').style.display='inline'; document.getElementById('Highlighter_383_835_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_383_835_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_383_835_Closed_Text').style.display='none'; document.getElementById('Highlighter_383_835_Open_Image').style.display='inline'; document.getElementById('Highlighter_383_835_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_383_835_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_383_835_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">0</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">3</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">2</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">4</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">3</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">5</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">4</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">7</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">5</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">9</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">6</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">11</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">7</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">21</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">8</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">24</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1[</span><span style="color: #800080;">9</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">30</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">0</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">5</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">2</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">7</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">3</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">8</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">4</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">10</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">5</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">17</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">6</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">18</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">7</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">23</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">8</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">27</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2[</span><span style="color: #800080;">9</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">30</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> count</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">; count</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">MAX; count</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />
<img id="Highlighter_786_818_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_786_818_Open_Text').style.display='none'; document.getElementById('Highlighter_786_818_Closed_Image').style.display='inline'; document.getElementById('Highlighter_786_818_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_786_818_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_786_818_Closed_Text').style.display='none'; document.getElementById('Highlighter_786_818_Open_Image').style.display='inline'; document.getElementById('Highlighter_786_818_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_786_818_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_786_818_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> result[count]</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> find()<br />
<img id="Highlighter_848_1410_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_848_1410_Open_Text').style.display='none'; document.getElementById('Highlighter_848_1410_Closed_Image').style.display='inline'; document.getElementById('Highlighter_848_1410_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_848_1410_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_848_1410_Closed_Text').style.display='none'; document.getElementById('Highlighter_848_1410_Open_Image').style.display='inline'; document.getElementById('Highlighter_848_1410_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_848_1410_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_848_1410_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> num1Index </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> num2Index </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> resultIndex </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(num1Index </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> MAX </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> num2Index </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> MAX)<br />
<img id="Highlighter_972_1393_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_972_1393_Open_Text').style.display='none'; document.getElementById('Highlighter_972_1393_Closed_Image').style.display='inline'; document.getElementById('Highlighter_972_1393_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_972_1393_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_972_1393_Closed_Text').style.display='none'; document.getElementById('Highlighter_972_1393_Open_Image').style.display='inline'; document.getElementById('Highlighter_972_1393_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_972_1393_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_972_1393_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(num1[num1Index] </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> num2[num2Index])<br />
<img id="Highlighter_1028_1064_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1028_1064_Open_Text').style.display='none'; document.getElementById('Highlighter_1028_1064_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1028_1064_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1028_1064_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1028_1064_Closed_Text').style.display='none'; document.getElementById('Highlighter_1028_1064_Open_Image').style.display='inline'; document.getElementById('Highlighter_1028_1064_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1028_1064_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1028_1064_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1Index</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(num1[num1Index] </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> num2[num2Index])<br />
<img id="Highlighter_1124_1160_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1124_1160_Open_Text').style.display='none'; document.getElementById('Highlighter_1124_1160_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1124_1160_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1124_1160_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1124_1160_Closed_Text').style.display='none'; document.getElementById('Highlighter_1124_1160_Open_Image').style.display='inline'; document.getElementById('Highlighter_1124_1160_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1124_1160_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1124_1160_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2Index</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(num1[num1Index] </span><span style="color: #000000;">==</span><span style="color: #000000;"> num2[num2Index])<br />
<img id="Highlighter_1221_1387_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1221_1387_Open_Text').style.display='none'; document.getElementById('Highlighter_1221_1387_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1221_1387_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1221_1387_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1221_1387_Closed_Text').style.display='none'; document.getElementById('Highlighter_1221_1387_Open_Image').style.display='inline'; document.getElementById('Highlighter_1221_1387_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1221_1387_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1221_1387_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> result[resultIndex] </span><span style="color: #000000;">=</span><span style="color: #000000;"> num1[num1Index];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> resultIndex</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num1Index</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> num2Index</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> resultCount</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> print()<br />
<img id="Highlighter_1424_1579_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1424_1579_Open_Text').style.display='none'; document.getElementById('Highlighter_1424_1579_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1424_1579_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1424_1579_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1424_1579_Closed_Text').style.display='none'; document.getElementById('Highlighter_1424_1579_Open_Image').style.display='inline'; document.getElementById('Highlighter_1424_1579_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_1424_1579_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1424_1579_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> count</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">; count</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">MAX; count</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />
<img id="Highlighter_1476_1518_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1476_1518_Open_Text').style.display='none'; document.getElementById('Highlighter_1476_1518_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1476_1518_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1476_1518_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1476_1518_Closed_Text').style.display='none'; document.getElementById('Highlighter_1476_1518_Open_Image').style.display='inline'; document.getElementById('Highlighter_1476_1518_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1476_1518_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1476_1518_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">result[count]</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #800000;">&#8220;</span><span style="color: #800000;">Result is </span><span style="color: #800000;">&#8220;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">resultCount</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jguoer.com/blog/index.php/archives/142/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>随机，随机</title>
		<link>http://www.jguoer.com/blog/index.php/archives/210</link>
		<comments>http://www.jguoer.com/blog/index.php/archives/210#comments</comments>
		<pubDate>Sun, 13 Dec 2009 18:21:00 +0000</pubDate>
		<dc:creator>诡异的西红柿</dc:creator>
				<category><![CDATA[趣题]]></category>

		<guid isPermaLink="false">/post/2009/12/13/Random-Random.aspx</guid>
		<description><![CDATA[题目：从M个数中随机的选取N个数。

提示：
<ul>
	<li>遍历一次（至少要在O（n））。</li>
	<li>保证随机（数学上概率相等）。</li>
	<li>不允许使用随机函数（不是不允许使用，但是注意，不允许在M个数里面利用随机函数随机的取N个数，那就没意义了）。</li>
</ul>
同样，我把C#/C++代码发在下面。思路在C#代码里面，可以先看思路。这也是我自己和朋友同事讨论的一个方法，不是最好，欢迎讨论。
<div class="code">
<div><span style="color: #000000;"> <img id="Highlighter_34_2686_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_34_2686_Closed_Text').style.display='none'; document.getElementById('Highlighter_34_2686_Open_Image').style.display='inline'; document.getElementById('Highlighter_34_2686_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_34_2686_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_34_2686_Open_Text').style.display='none'; document.getElementById('Highlighter_34_2686_Closed_Image').style.display='inline'; document.getElementById('Highlighter_34_2686_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /></span><span id="Highlighter_34_2686_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">C# Code</span><span id="Highlighter_34_2686_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> C# Code</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img id="Highlighter_51_138_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_51_138_Closed_Text').style.display='none'; document.getElementById('Highlighter_51_138_Open_Image').style.display='inline'; document.getElementById('Highlighter_51_138_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_51_138_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_51_138_Open_Text').style.display='none'; document.getElementById('Highlighter_51_138_Closed_Image').style.display='inline'; document.getElementById('Highlighter_51_138_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /></span><span id="Highlighter_51_138_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">思路</span><span id="Highlighter_51_138_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 思路</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">遍历一次，并给每个数字一个随机的权值
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">创建一个堆，并通过权值维护这个堆（大根堆）
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">从大根堆里取出N个数即随机的取出</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img id="Highlighter_140_2674_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_140_2674_Closed_Text').style.display='none'; document.getElementById('Highlighter_140_2674_Open_Image').style.display='inline'; document.getElementById('Highlighter_140_2674_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_140_2674_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_140_2674_Open_Text').style.display='none'; document.getElementById('Highlighter_140_2674_Closed_Image').style.display='inline'; document.getElementById('Highlighter_140_2674_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /></span><span id="Highlighter_140_2674_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Code</span><span id="Highlighter_140_2674_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Code</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> Finder
<img id="Highlighter_171_2662_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_171_2662_Open_Text').style.display='none'; document.getElementById('Highlighter_171_2662_Closed_Image').style.display='inline'; document.getElementById('Highlighter_171_2662_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_171_2662_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_171_2662_Closed_Text').style.display='none'; document.getElementById('Highlighter_171_2662_Open_Image').style.display='inline'; document.getElementById('Highlighter_171_2662_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_171_2662_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_171_2662_Open_Text"><span style="color: #000000;">{
<img id="Highlighter_177_320_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_177_320_Closed_Text').style.display='none'; document.getElementById('Highlighter_177_320_Open_Image').style.display='inline'; document.getElementById('Highlighter_177_320_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_177_320_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_177_320_Open_Text').style.display='none'; document.getElementById('Highlighter_177_320_Closed_Image').style.display='inline'; document.getElementById('Highlighter_177_320_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_177_320_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">自定义Array</span><span id="Highlighter_177_320_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 自定义Array</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">自定义的数据类型，用于堆</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Array
<img id="Highlighter_241_304_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_241_304_Open_Text').style.display='none'; document.getElementById('Highlighter_241_304_Closed_Image').style.display='inline'; document.getElementById('Highlighter_241_304_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_241_304_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_241_304_Closed_Text').style.display='none'; document.getElementById('Highlighter_241_304_Open_Image').style.display='inline'; document.getElementById('Highlighter_241_304_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_241_304_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_241_304_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> randonData;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img id="Highlighter_326_1580_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_326_1580_Closed_Text').style.display='none'; document.getElementById('Highlighter_326_1580_Open_Image').style.display='inline'; document.getElementById('Highlighter_326_1580_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_326_1580_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_326_1580_Open_Text').style.display='none'; document.getElementById('Highlighter_326_1580_Closed_Image').style.display='inline'; document.getElementById('Highlighter_326_1580_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_326_1580_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Heap</span><span id="Highlighter_326_1580_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Heap</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Heap
<img id="Highlighter_366_1564_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_366_1564_Open_Text').style.display='none'; document.getElementById('Highlighter_366_1564_Closed_Image').style.display='inline'; document.getElementById('Highlighter_366_1564_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_366_1564_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_366_1564_Closed_Text').style.display='none'; document.getElementById('Highlighter_366_1564_Open_Image').style.display='inline'; document.getElementById('Highlighter_366_1564_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_366_1564_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_366_1564_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Array[] _array;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Array[] Init(Array[] array)
<img id="Highlighter_452_546_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_452_546_Open_Text').style.display='none'; document.getElementById('Highlighter_452_546_Closed_Image').style.display='inline'; document.getElementById('Highlighter_452_546_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_452_546_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_452_546_Closed_Text').style.display='none'; document.getElementById('Highlighter_452_546_Open_Image').style.display='inline'; document.getElementById('Highlighter_452_546_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_452_546_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_452_546_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> _array </span><span style="color: #000000;">=</span><span style="color: #000000;"> array;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> GenerateHeap();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> _array;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> GenerateHeap()
<img id="Highlighter_592_745_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_592_745_Open_Text').style.display='none'; document.getElementById('Highlighter_592_745_Closed_Image').style.display='inline'; document.getElementById('Highlighter_592_745_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_592_745_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_592_745_Closed_Text').style.display='none'; document.getElementById('Highlighter_592_745_Open_Image').style.display='inline'; document.getElementById('Highlighter_592_745_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_592_745_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_592_745_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> _array.Length </span><span style="color: #000000;">/</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">; i </span><span style="color: #000000;">&#62;=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i</span><span style="color: #000000;">--</span><span style="color: #000000;">)
<img id="Highlighter_667_735_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_667_735_Open_Text').style.display='none'; document.getElementById('Highlighter_667_735_Closed_Image').style.display='inline'; document.getElementById('Highlighter_667_735_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_667_735_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_667_735_Closed_Text').style.display='none'; document.getElementById('Highlighter_667_735_Open_Image').style.display='inline'; document.getElementById('Highlighter_667_735_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_667_735_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_667_735_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> DoMaxHeap(_array, _array.Length, i);
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Swap(Array[] array,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> big)
<img id="Highlighter_810_954_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_810_954_Open_Text').style.display='none'; document.getElementById('Highlighter_810_954_Closed_Image').style.display='inline'; document.getElementById('Highlighter_810_954_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_810_954_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_810_954_Closed_Text').style.display='none'; document.getElementById('Highlighter_810_954_Open_Image').style.display='inline'; document.getElementById('Highlighter_810_954_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_810_954_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_810_954_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Array temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Array();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> array[i];
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> array[i] </span><span style="color: #000000;">=</span><span style="color: #000000;"> array[big];
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> array[big] </span><span style="color: #000000;">=</span><span style="color: #000000;"> temp;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> DoMaxHeap(Array[] array,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> heapSize,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i)
<img id="Highlighter_1029_1558_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1029_1558_Open_Text').style.display='none'; document.getElementById('Highlighter_1029_1558_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1029_1558_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1029_1558_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1029_1558_Closed_Text').style.display='none'; document.getElementById('Highlighter_1029_1558_Open_Image').style.display='inline'; document.getElementById('Highlighter_1029_1558_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1029_1558_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1029_1558_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> left </span><span style="color: #000000;">=</span><span style="color: #000000;"> i </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> right </span><span style="color: #000000;">=</span><span style="color: #000000;"> i </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> big </span><span style="color: #000000;">=</span><span style="color: #000000;"> i;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (left </span><span style="color: #000000;">&#60;</span><span style="color: #000000;"> heapSize </span><span style="color: #000000;">&#38;&#38;</span><span style="color: #000000;"> array[left].randonData </span><span style="color: #000000;">&#62;</span><span style="color: #000000;"> array[big].randonData)
<img id="Highlighter_1221_1264_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1221_1264_Open_Text').style.display='none'; document.getElementById('Highlighter_1221_1264_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1221_1264_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1221_1264_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1221_1264_Closed_Text').style.display='none'; document.getElementById('Highlighter_1221_1264_Open_Image').style.display='inline'; document.getElementById('Highlighter_1221_1264_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1221_1264_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1221_1264_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> big </span><span s]]></description>
			<content:encoded><![CDATA[<p>题目：从M个数中随机的选取N个数。</p>
<p>提示：</p>
<ul>
<li>遍历一次（至少要在O（n））。</li>
<li>保证随机（数学上概率相等）。</li>
<li>不允许使用随机函数（不是不允许使用，但是注意，不允许在M个数里面利用随机函数随机的取N个数，那就没意义了）。</li>
</ul>
<p>同样，我把C#/C++代码发在下面。思路在C#代码里面，可以先看思路。这也是我自己和朋友同事讨论的一个方法，不是最好，欢迎讨论。</p>
<div class="code">
<div><span style="color: #000000;"> <img id="Highlighter_34_2686_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_34_2686_Closed_Text').style.display='none'; document.getElementById('Highlighter_34_2686_Open_Image').style.display='inline'; document.getElementById('Highlighter_34_2686_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_34_2686_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_34_2686_Open_Text').style.display='none'; document.getElementById('Highlighter_34_2686_Closed_Image').style.display='inline'; document.getElementById('Highlighter_34_2686_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /></span><span id="Highlighter_34_2686_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">C# Code</span><span id="Highlighter_34_2686_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> C# Code</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img id="Highlighter_51_138_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_51_138_Closed_Text').style.display='none'; document.getElementById('Highlighter_51_138_Open_Image').style.display='inline'; document.getElementById('Highlighter_51_138_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_51_138_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_51_138_Open_Text').style.display='none'; document.getElementById('Highlighter_51_138_Closed_Image').style.display='inline'; document.getElementById('Highlighter_51_138_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /></span><span id="Highlighter_51_138_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">思路</span><span id="Highlighter_51_138_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 思路</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">遍历一次，并给每个数字一个随机的权值<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">创建一个堆，并通过权值维护这个堆（大根堆）<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">从大根堆里取出N个数即随机的取出</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img id="Highlighter_140_2674_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_140_2674_Closed_Text').style.display='none'; document.getElementById('Highlighter_140_2674_Open_Image').style.display='inline'; document.getElementById('Highlighter_140_2674_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_140_2674_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_140_2674_Open_Text').style.display='none'; document.getElementById('Highlighter_140_2674_Closed_Image').style.display='inline'; document.getElementById('Highlighter_140_2674_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /></span><span id="Highlighter_140_2674_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Code</span><span id="Highlighter_140_2674_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Code</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> Finder<br />
<img id="Highlighter_171_2662_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_171_2662_Open_Text').style.display='none'; document.getElementById('Highlighter_171_2662_Closed_Image').style.display='inline'; document.getElementById('Highlighter_171_2662_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_171_2662_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_171_2662_Closed_Text').style.display='none'; document.getElementById('Highlighter_171_2662_Open_Image').style.display='inline'; document.getElementById('Highlighter_171_2662_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_171_2662_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_171_2662_Open_Text"><span style="color: #000000;">{<br />
<img id="Highlighter_177_320_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_177_320_Closed_Text').style.display='none'; document.getElementById('Highlighter_177_320_Open_Image').style.display='inline'; document.getElementById('Highlighter_177_320_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_177_320_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_177_320_Open_Text').style.display='none'; document.getElementById('Highlighter_177_320_Closed_Image').style.display='inline'; document.getElementById('Highlighter_177_320_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_177_320_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">自定义Array</span><span id="Highlighter_177_320_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 自定义Array</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">自定义的数据类型，用于堆</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Array<br />
<img id="Highlighter_241_304_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_241_304_Open_Text').style.display='none'; document.getElementById('Highlighter_241_304_Closed_Image').style.display='inline'; document.getElementById('Highlighter_241_304_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_241_304_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_241_304_Closed_Text').style.display='none'; document.getElementById('Highlighter_241_304_Open_Image').style.display='inline'; document.getElementById('Highlighter_241_304_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_241_304_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_241_304_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> randonData;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img id="Highlighter_326_1580_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_326_1580_Closed_Text').style.display='none'; document.getElementById('Highlighter_326_1580_Open_Image').style.display='inline'; document.getElementById('Highlighter_326_1580_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_326_1580_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_326_1580_Open_Text').style.display='none'; document.getElementById('Highlighter_326_1580_Closed_Image').style.display='inline'; document.getElementById('Highlighter_326_1580_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_326_1580_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Heap</span><span id="Highlighter_326_1580_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Heap</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Heap<br />
<img id="Highlighter_366_1564_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_366_1564_Open_Text').style.display='none'; document.getElementById('Highlighter_366_1564_Closed_Image').style.display='inline'; document.getElementById('Highlighter_366_1564_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_366_1564_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_366_1564_Closed_Text').style.display='none'; document.getElementById('Highlighter_366_1564_Open_Image').style.display='inline'; document.getElementById('Highlighter_366_1564_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_366_1564_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_366_1564_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Array[] _array;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Array[] Init(Array[] array)<br />
<img id="Highlighter_452_546_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_452_546_Open_Text').style.display='none'; document.getElementById('Highlighter_452_546_Closed_Image').style.display='inline'; document.getElementById('Highlighter_452_546_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_452_546_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_452_546_Closed_Text').style.display='none'; document.getElementById('Highlighter_452_546_Open_Image').style.display='inline'; document.getElementById('Highlighter_452_546_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_452_546_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_452_546_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> _array </span><span style="color: #000000;">=</span><span style="color: #000000;"> array;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> GenerateHeap();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> _array;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> GenerateHeap()<br />
<img id="Highlighter_592_745_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_592_745_Open_Text').style.display='none'; document.getElementById('Highlighter_592_745_Closed_Image').style.display='inline'; document.getElementById('Highlighter_592_745_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_592_745_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_592_745_Closed_Text').style.display='none'; document.getElementById('Highlighter_592_745_Open_Image').style.display='inline'; document.getElementById('Highlighter_592_745_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_592_745_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_592_745_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> _array.Length </span><span style="color: #000000;">/</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">; i </span><span style="color: #000000;">&gt;=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i</span><span style="color: #000000;">&#8211;</span><span style="color: #000000;">)<br />
<img id="Highlighter_667_735_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_667_735_Open_Text').style.display='none'; document.getElementById('Highlighter_667_735_Closed_Image').style.display='inline'; document.getElementById('Highlighter_667_735_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_667_735_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_667_735_Closed_Text').style.display='none'; document.getElementById('Highlighter_667_735_Open_Image').style.display='inline'; document.getElementById('Highlighter_667_735_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_667_735_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_667_735_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> DoMaxHeap(_array, _array.Length, i);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Swap(Array[] array,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> big)<br />
<img id="Highlighter_810_954_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_810_954_Open_Text').style.display='none'; document.getElementById('Highlighter_810_954_Closed_Image').style.display='inline'; document.getElementById('Highlighter_810_954_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_810_954_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_810_954_Closed_Text').style.display='none'; document.getElementById('Highlighter_810_954_Open_Image').style.display='inline'; document.getElementById('Highlighter_810_954_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_810_954_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_810_954_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Array temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Array();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> array[i];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> array[i] </span><span style="color: #000000;">=</span><span style="color: #000000;"> array[big];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> array[big] </span><span style="color: #000000;">=</span><span style="color: #000000;"> temp;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> DoMaxHeap(Array[] array,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> heapSize,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i)<br />
<img id="Highlighter_1029_1558_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1029_1558_Open_Text').style.display='none'; document.getElementById('Highlighter_1029_1558_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1029_1558_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1029_1558_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1029_1558_Closed_Text').style.display='none'; document.getElementById('Highlighter_1029_1558_Open_Image').style.display='inline'; document.getElementById('Highlighter_1029_1558_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1029_1558_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1029_1558_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> left </span><span style="color: #000000;">=</span><span style="color: #000000;"> i </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> right </span><span style="color: #000000;">=</span><span style="color: #000000;"> i </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> big </span><span style="color: #000000;">=</span><span style="color: #000000;"> i;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (left </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> heapSize </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> array[left].randonData </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> array[big].randonData)<br />
<img id="Highlighter_1221_1264_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1221_1264_Open_Text').style.display='none'; document.getElementById('Highlighter_1221_1264_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1221_1264_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1221_1264_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1221_1264_Closed_Text').style.display='none'; document.getElementById('Highlighter_1221_1264_Open_Image').style.display='inline'; document.getElementById('Highlighter_1221_1264_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1221_1264_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1221_1264_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> big </span><span style="color: #000000;">=</span><span style="color: #000000;"> left;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (right </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> heapSize </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> array[right].randonData </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> array[big].randonData)<br />
<img id="Highlighter_1363_1407_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1363_1407_Open_Text').style.display='none'; document.getElementById('Highlighter_1363_1407_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1363_1407_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1363_1407_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1363_1407_Closed_Text').style.display='none'; document.getElementById('Highlighter_1363_1407_Open_Image').style.display='inline'; document.getElementById('Highlighter_1363_1407_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1363_1407_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1363_1407_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> big </span><span style="color: #000000;">=</span><span style="color: #000000;"> right;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (i </span><span style="color: #000000;">!=</span><span style="color: #000000;"> big)<br />
<img id="Highlighter_1447_1548_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1447_1548_Open_Text').style.display='none'; document.getElementById('Highlighter_1447_1548_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1447_1548_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1447_1548_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1447_1548_Closed_Text').style.display='none'; document.getElementById('Highlighter_1447_1548_Open_Image').style.display='inline'; document.getElementById('Highlighter_1447_1548_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1447_1548_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1447_1548_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Swap(array, i, big);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> DoMaxHeap(array, heapSize, big);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program<br />
<img id="Highlighter_1604_2660_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1604_2660_Open_Text').style.display='none'; document.getElementById('Highlighter_1604_2660_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1604_2660_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1604_2660_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1604_2660_Closed_Text').style.display='none'; document.getElementById('Highlighter_1604_2660_Open_Image').style.display='inline'; document.getElementById('Highlighter_1604_2660_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1604_2660_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1604_2660_Open_Text"><span style="color: #000000;">{<br />
<img id="Highlighter_1614_1713_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1614_1713_Closed_Text').style.display='none'; document.getElementById('Highlighter_1614_1713_Open_Image').style.display='inline'; document.getElementById('Highlighter_1614_1713_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_1614_1713_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1614_1713_Open_Text').style.display='none'; document.getElementById('Highlighter_1614_1713_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1614_1713_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_1614_1713_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">待输入的数字</span><span id="Highlighter_1614_1713_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 待输入的数字</span><span style="color: #000000;"><br />
<img id="Highlighter_1660_1693_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1660_1693_Open_Text').style.display='none'; document.getElementById('Highlighter_1660_1693_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1660_1693_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1660_1693_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1660_1693_Closed_Text').style.display='none'; document.getElementById('Highlighter_1660_1693_Open_Image').style.display='inline'; document.getElementById('Highlighter_1660_1693_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[] numbers </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span id="Highlighter_1660_1693_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1660_1693_Open_Text"><span style="color: #000000;">{ </span><span style="color: #800080;">1</span><span style="color: #000000;">, </span><span style="color: #800080;">2</span><span style="color: #000000;">, </span><span style="color: #800080;">3</span><span style="color: #000000;">, </span><span style="color: #800080;">4</span><span style="color: #000000;">, </span><span style="color: #800080;">5</span><span style="color: #000000;">, </span><span style="color: #800080;">6</span><span style="color: #000000;">, </span><span style="color: #800080;">7</span><span style="color: #000000;">, </span><span style="color: #800080;">8</span><span style="color: #000000;">, </span><span style="color: #800080;">9</span><span style="color: #000000;">, </span><span style="color: #800080;">10</span><span style="color: #000000;"> }</span></span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> Array[] arrays </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Array[</span><span style="color: #800080;">10</span><span style="color: #000000;">];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Main(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)<br />
<img id="Highlighter_1811_2086_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1811_2086_Open_Text').style.display='none'; document.getElementById('Highlighter_1811_2086_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1811_2086_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1811_2086_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1811_2086_Closed_Text').style.display='none'; document.getElementById('Highlighter_1811_2086_Open_Image').style.display='inline'; document.getElementById('Highlighter_1811_2086_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1811_2086_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1811_2086_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Heap heap </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Heap();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">给每个数字一个权值并创建成为自定义的Array类型</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> GenerateArray();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> OutPut(</span><span style="color: #800080;">10</span><span style="color: #000000;">);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Console.WriteLine(</span><span style="color: #800000;">&#8220;</span><span style="color: #800000;">Find 5 Number</span><span style="color: #800000;">&#8220;</span><span style="color: #000000;">);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">生成并输出5个数字</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> arrays </span><span style="color: #000000;">=</span><span style="color: #000000;"> heap.Init(arrays);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> OutPut(</span><span style="color: #800080;">5</span><span style="color: #000000;">);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> GenerateArray()<br />
<img id="Highlighter_2132_2411_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2132_2411_Open_Text').style.display='none'; document.getElementById('Highlighter_2132_2411_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2132_2411_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2132_2411_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2132_2411_Closed_Text').style.display='none'; document.getElementById('Highlighter_2132_2411_Open_Image').style.display='inline'; document.getElementById('Highlighter_2132_2411_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2132_2411_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2132_2411_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Random random </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Random();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> numbers.Length ; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />
<img id="Highlighter_2243_2401_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2243_2401_Open_Text').style.display='none'; document.getElementById('Highlighter_2243_2401_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2243_2401_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2243_2401_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2243_2401_Closed_Text').style.display='none'; document.getElementById('Highlighter_2243_2401_Open_Image').style.display='inline'; document.getElementById('Highlighter_2243_2401_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2243_2401_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2243_2401_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> arrays[i] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Array();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> arrays[i].data </span><span style="color: #000000;">=</span><span style="color: #000000;"> numbers[i];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> arrays[i].randonData </span><span style="color: #000000;">=</span><span style="color: #000000;"> random.Next(</span><span style="color: #800080;">100</span><span style="color: #000000;">);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> OutPut(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n)<br />
<img id="Highlighter_2455_2654_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2455_2654_Open_Text').style.display='none'; document.getElementById('Highlighter_2455_2654_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2455_2654_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2455_2654_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2455_2654_Closed_Text').style.display='none'; document.getElementById('Highlighter_2455_2654_Open_Image').style.display='inline'; document.getElementById('Highlighter_2455_2654_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2455_2654_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2455_2654_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> arrays.Length </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> i </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> n; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />
<img id="Highlighter_2530_2644_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2530_2644_Open_Text').style.display='none'; document.getElementById('Highlighter_2530_2644_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2530_2644_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2530_2644_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2530_2644_Closed_Text').style.display='none'; document.getElementById('Highlighter_2530_2644_Open_Image').style.display='inline'; document.getElementById('Highlighter_2530_2644_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2530_2644_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2530_2644_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Console.WriteLine(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">.Format(</span><span style="color: #800000;">&#8220;</span><span style="color: #800000;">{0},{1}</span><span style="color: #800000;">&#8220;</span><span style="color: #000000;">, arrays[i].data, arrays[i].randonData));<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span></div>
</div>
<div class="code" style="margin-top: 10px;">
<div><!--</p>
<p>Code highlighting produced by Actipro CodeHighlighter (freeware)</p>
<p>http://www.CodeHighlighter.com/</p>
<p>--><img id="Highlighter_0_1627_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_0_1627_Closed_Text').style.display='none'; document.getElementById('Highlighter_0_1627_Open_Image').style.display='inline'; document.getElementById('Highlighter_0_1627_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_0_1627_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_0_1627_Open_Text').style.display='none'; document.getElementById('Highlighter_0_1627_Closed_Image').style.display='inline'; document.getElementById('Highlighter_0_1627_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><span id="Highlighter_0_1627_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">C++ Code</span><span id="Highlighter_0_1627_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> C++ Code</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />#include </span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />#include </span><span style="color: #000000;">&lt;</span><span style="color: #000000;">time.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;"> MAX 10</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">using</span><span style="color: #000000;"> </span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> std;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> Array<br />
<img id="Highlighter_115_146_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_115_146_Open_Text').style.display='none'; document.getElementById('Highlighter_115_146_Closed_Image').style.display='inline'; document.getElementById('Highlighter_115_146_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_115_146_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_115_146_Closed_Text').style.display='none'; document.getElementById('Highlighter_115_146_Open_Image').style.display='inline'; document.getElementById('Highlighter_115_146_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_115_146_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_115_146_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> randomData;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">Array;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Heap<br />
<img id="Highlighter_165_351_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_165_351_Open_Text').style.display='none'; document.getElementById('Highlighter_165_351_Closed_Image').style.display='inline'; document.getElementById('Highlighter_165_351_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_165_351_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_165_351_Closed_Text').style.display='none'; document.getElementById('Highlighter_165_351_Open_Image').style.display='inline'; document.getElementById('Highlighter_165_351_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_165_351_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_165_351_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;">:<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Heap();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Array </span><span style="color: #000000;">*</span><span style="color: #000000;">Init(Array </span><span style="color: #000000;">*</span><span style="color: #000000;">array);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">private</span><span style="color: #000000;">:<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Array </span><span style="color: #000000;">*</span><span style="color: #000000;">_array;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> GenerateHeap();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Swap(Array </span><span style="color: #000000;">*</span><span style="color: #000000;">array,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> big);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> DoMaxHeap(Array </span><span style="color: #000000;">*</span><span style="color: #000000;">array,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />Heap::Heap()<br />
<img id="Highlighter_367_369_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_367_369_Open_Text').style.display='none'; document.getElementById('Highlighter_367_369_Closed_Image').style.display='inline'; document.getElementById('Highlighter_367_369_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_367_369_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_367_369_Closed_Text').style.display='none'; document.getElementById('Highlighter_367_369_Open_Image').style.display='inline'; document.getElementById('Highlighter_367_369_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_367_369_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_367_369_Open_Text"><span style="color: #000000;">{}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />Array </span><span style="color: #000000;">*</span><span style="color: #000000;">Heap::Init(Array </span><span style="color: #000000;">*</span><span style="color: #000000;">array)<br />
<img id="Highlighter_403_459_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_403_459_Open_Text').style.display='none'; document.getElementById('Highlighter_403_459_Closed_Image').style.display='inline'; document.getElementById('Highlighter_403_459_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_403_459_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_403_459_Closed_Text').style.display='none'; document.getElementById('Highlighter_403_459_Open_Image').style.display='inline'; document.getElementById('Highlighter_403_459_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_403_459_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_403_459_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> _array </span><span style="color: #000000;">=</span><span style="color: #000000;"> array;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> GenerateHeap();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> _array;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Heap::GenerateHeap()<br />
<img id="Highlighter_487_552_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_487_552_Open_Text').style.display='none'; document.getElementById('Highlighter_487_552_Closed_Image').style.display='inline'; document.getElementById('Highlighter_487_552_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_487_552_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_487_552_Closed_Text').style.display='none'; document.getElementById('Highlighter_487_552_Open_Image').style.display='inline'; document.getElementById('Highlighter_487_552_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_487_552_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_487_552_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i</span><span style="color: #000000;">=</span><span style="color: #000000;">MAX</span><span style="color: #000000;">/</span><span style="color: #800080;">2</span><span style="color: #000000;">-</span><span style="color: #800080;">1</span><span style="color: #000000;">;i</span><span style="color: #000000;">&gt;=</span><span style="color: #800080;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;">&#8211;</span><span style="color: #000000;">)<br />
<img id="Highlighter_519_550_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_519_550_Open_Text').style.display='none'; document.getElementById('Highlighter_519_550_Closed_Image').style.display='inline'; document.getElementById('Highlighter_519_550_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_519_550_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_519_550_Closed_Text').style.display='none'; document.getElementById('Highlighter_519_550_Open_Image').style.display='inline'; document.getElementById('Highlighter_519_550_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_519_550_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_519_550_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> DoMaxHeap(_array,MAX,i);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Heap::Swap(Array </span><span style="color: #000000;">*</span><span style="color: #000000;">array,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> big)<br />
<img id="Highlighter_598_676_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_598_676_Open_Text').style.display='none'; document.getElementById('Highlighter_598_676_Closed_Image').style.display='inline'; document.getElementById('Highlighter_598_676_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_598_676_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_598_676_Closed_Text').style.display='none'; document.getElementById('Highlighter_598_676_Open_Image').style.display='inline'; document.getElementById('Highlighter_598_676_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_598_676_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_598_676_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Array temp;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> array[i];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> array[i] </span><span style="color: #000000;">=</span><span style="color: #000000;"> array[big];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> array[big] </span><span style="color: #000000;">=</span><span style="color: #000000;"> temp;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Heap::DoMaxHeap(Array </span><span style="color: #000000;">*</span><span style="color: #000000;">array,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> heapsize,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i)<br />
<img id="Highlighter_732_1039_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_732_1039_Open_Text').style.display='none'; document.getElementById('Highlighter_732_1039_Closed_Image').style.display='inline'; document.getElementById('Highlighter_732_1039_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_732_1039_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_732_1039_Closed_Text').style.display='none'; document.getElementById('Highlighter_732_1039_Open_Image').style.display='inline'; document.getElementById('Highlighter_732_1039_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_732_1039_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_732_1039_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> left </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">i</span><span style="color: #000000;">+</span><span style="color: #800080;">1</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> right </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">i</span><span style="color: #000000;">+</span><span style="color: #800080;">2</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> big </span><span style="color: #000000;">=</span><span style="color: #000000;"> i;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(left</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">heapsize</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">array[left].randomData</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">array[big].randomData)<br />
<img id="Highlighter_857_875_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_857_875_Open_Text').style.display='none'; document.getElementById('Highlighter_857_875_Closed_Image').style.display='inline'; document.getElementById('Highlighter_857_875_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_857_875_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_857_875_Closed_Text').style.display='none'; document.getElementById('Highlighter_857_875_Open_Image').style.display='inline'; document.getElementById('Highlighter_857_875_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_857_875_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_857_875_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> big </span><span style="color: #000000;">=</span><span style="color: #000000;"> left;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(right</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">heapsize</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">array[right].randomData</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">array[big].randomData)<br />
<img id="Highlighter_945_964_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_945_964_Open_Text').style.display='none'; document.getElementById('Highlighter_945_964_Closed_Image').style.display='inline'; document.getElementById('Highlighter_945_964_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_945_964_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_945_964_Closed_Text').style.display='none'; document.getElementById('Highlighter_945_964_Open_Image').style.display='inline'; document.getElementById('Highlighter_945_964_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_945_964_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_945_964_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> big </span><span style="color: #000000;">=</span><span style="color: #000000;"> right;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(i</span><span style="color: #000000;">!=</span><span style="color: #000000;">big)<br />
<img id="Highlighter_979_1037_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_979_1037_Open_Text').style.display='none'; document.getElementById('Highlighter_979_1037_Closed_Image').style.display='inline'; document.getElementById('Highlighter_979_1037_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_979_1037_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_979_1037_Closed_Text').style.display='none'; document.getElementById('Highlighter_979_1037_Open_Image').style.display='inline'; document.getElementById('Highlighter_979_1037_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_979_1037_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_979_1037_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Swap(array,i,big);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> DoMaxHeap(array,heapsize,big);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> GenerateArray();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> OutPut(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img id="Highlighter_1103_1125_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1103_1125_Open_Text').style.display='none'; document.getElementById('Highlighter_1103_1125_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1103_1125_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1103_1125_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1103_1125_Closed_Text').style.display='none'; document.getElementById('Highlighter_1103_1125_Open_Image').style.display='inline'; document.getElementById('Highlighter_1103_1125_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> numbers[MAX] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span id="Highlighter_1103_1125_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1103_1125_Open_Text"><span style="color: #000000;">{</span><span style="color: #800080;">1</span><span style="color: #000000;">,</span><span style="color: #800080;">2</span><span style="color: #000000;">,</span><span style="color: #800080;">3</span><span style="color: #000000;">,</span><span style="color: #800080;">4</span><span style="color: #000000;">,</span><span style="color: #800080;">5</span><span style="color: #000000;">,</span><span style="color: #800080;">6</span><span style="color: #000000;">,</span><span style="color: #800080;">7</span><span style="color: #000000;">,</span><span style="color: #800080;">8</span><span style="color: #000000;">,</span><span style="color: #800080;">9</span><span style="color: #000000;">,</span><span style="color: #800080;">10</span><span style="color: #000000;">}</span></span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />Array </span><span style="color: #000000;">*</span><span style="color: #000000;">array;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main()<br />
<img id="Highlighter_1153_1288_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1153_1288_Open_Text').style.display='none'; document.getElementById('Highlighter_1153_1288_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1153_1288_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1153_1288_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1153_1288_Closed_Text').style.display='none'; document.getElementById('Highlighter_1153_1288_Open_Image').style.display='inline'; document.getElementById('Highlighter_1153_1288_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_1153_1288_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1153_1288_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Heap </span><span style="color: #000000;">*</span><span style="color: #000000;">heap </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Heap();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> GenerateArray();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> OutPut(MAX);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> heap</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">Init(array);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #800000;">&#8220;</span><span style="color: #800000;">Find 5 numbers</span><span style="color: #800000;">&#8220;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> OutPut(</span><span style="color: #800080;">5</span><span style="color: #000000;">);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> GenerateArray()<br />
<img id="Highlighter_1311_1434_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1311_1434_Open_Text').style.display='none'; document.getElementById('Highlighter_1311_1434_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1311_1434_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1311_1434_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1311_1434_Closed_Text').style.display='none'; document.getElementById('Highlighter_1311_1434_Open_Image').style.display='inline'; document.getElementById('Highlighter_1311_1434_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_1311_1434_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1311_1434_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> array </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Array[MAX];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">MAX;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />
<img id="Highlighter_1363_1432_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1363_1432_Open_Text').style.display='none'; document.getElementById('Highlighter_1363_1432_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1363_1432_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1363_1432_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1363_1432_Closed_Text').style.display='none'; document.getElementById('Highlighter_1363_1432_Open_Image').style.display='inline'; document.getElementById('Highlighter_1363_1432_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1363_1432_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1363_1432_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> array[i].data </span><span style="color: #000000;">=</span><span style="color: #000000;"> numbers[i];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> array[i].randomData </span><span style="color: #000000;">=</span><span style="color: #000000;"> rand()</span><span style="color: #000000;">%</span><span style="color: #800080;">10</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> OutPut(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n)<br />
<img id="Highlighter_1455_1615_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1455_1615_Open_Text').style.display='none'; document.getElementById('Highlighter_1455_1615_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1455_1615_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1455_1615_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1455_1615_Closed_Text').style.display='none'; document.getElementById('Highlighter_1455_1615_Open_Image').style.display='inline'; document.getElementById('Highlighter_1455_1615_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_1455_1615_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1455_1615_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">MAX)<br />
<img id="Highlighter_1469_1512_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1469_1512_Open_Text').style.display='none'; document.getElementById('Highlighter_1469_1512_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1469_1512_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1469_1512_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1469_1512_Closed_Text').style.display='none'; document.getElementById('Highlighter_1469_1512_Open_Image').style.display='inline'; document.getElementById('Highlighter_1469_1512_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1469_1512_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1469_1512_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #800000;">&#8220;</span><span style="color: #800000;">Error Input</span><span style="color: #800000;">&#8220;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">MAX</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">i</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">n;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />
<img id="Highlighter_1544_1613_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1544_1613_Open_Text').style.display='none'; document.getElementById('Highlighter_1544_1613_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1544_1613_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1544_1613_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1544_1613_Closed_Text').style.display='none'; document.getElementById('Highlighter_1544_1613_Open_Image').style.display='inline'; document.getElementById('Highlighter_1544_1613_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1544_1613_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1544_1613_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> cout</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #800000;">&#8220;</span><span style="color: #800000;">{</span><span style="color: #800000;">&#8220;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">array[i].data</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #800000;">&#8220;</span><span style="color: #800000;">,</span><span style="color: #800000;">&#8220;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">array[i].randomData</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #800000;">&#8220;</span><span style="color: #800000;">}</span><span style="color: #800000;">&#8220;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jguoer.com/blog/index.php/archives/210/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>这两天看到杂事</title>
		<link>http://www.jguoer.com/blog/index.php/archives/213</link>
		<comments>http://www.jguoer.com/blog/index.php/archives/213#comments</comments>
		<pubDate>Mon, 07 Dec 2009 23:53:00 +0000</pubDate>
		<dc:creator>诡异的西红柿</dc:creator>
				<category><![CDATA[小分享]]></category>
		<category><![CDATA[数据结构]]></category>
		<category><![CDATA[趣题]]></category>
		<category><![CDATA[软件开发]]></category>

		<guid isPermaLink="false">/post/2009/12/07/Something-Idea-Recent.aspx</guid>
		<description><![CDATA[<p>第一就是关于内存的速度的，其实不难，但是开发的时候想到的很少，能想到的优化的就是高人，我也是路过看到不错，觉得很有用，就贴过来了。</p>
<p style="text-align: center;"><img alt="" src="/blog/wp-content/uploads/2009/12/memory.jpg" /></p>
<p>另一个就是《算法导论》了，国外的教材，也是MIT的标准教材，很好很强大，昨天看到了堆排序和快速排序（100页），用了好多张纸证明线性比较排序的上界和下界，堆排序和快速排序的最好的速度是nlogn，从<strong>数学角度</strong>上证明了线性比较排序（如判断a[1]&#62;a[0]）的最快就是nlogn，也就是说只要你是比较的，而且还是线性的，nlogn就是最快的了。</p>
<p>数学公式太复杂，什么期望啊，概率啊，极限啊，记的不是很清楚，让我自己推倒也没这个能力，不过倒是让我知道了，以后别想线性快速的排序还能超过nlogn了。：）</p>]]></description>
			<content:encoded><![CDATA[<p>第一就是关于内存的速度的，其实不难，但是开发的时候想到的很少，能想到的优化的就是高人，我也是路过看到不错，觉得很有用，就贴过来了。</p>
<p style="text-align: center;"><img alt="" src="/blog/wp-content/uploads/2009/12/memory.jpg" /></p>
<p>另一个就是《算法导论》了，国外的教材，也是MIT的标准教材，很好很强大，昨天看到了堆排序和快速排序（100页），用了好多张纸证明线性比较排序的上界和下界，堆排序和快速排序的最好的速度是nlogn，从<strong>数学角度</strong>上证明了线性比较排序（如判断a[1]&gt;a[0]）的最快就是nlogn，也就是说只要你是比较的，而且还是线性的，nlogn就是最快的了。</p>
<p>数学公式太复杂，什么期望啊，概率啊，极限啊，记的不是很清楚，让我自己推倒也没这个能力，不过倒是让我知道了，以后别想线性快速的排序还能超过nlogn了。：）</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jguoer.com/blog/index.php/archives/213/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>LRU算法的一些用途</title>
		<link>http://www.jguoer.com/blog/index.php/archives/217</link>
		<comments>http://www.jguoer.com/blog/index.php/archives/217#comments</comments>
		<pubDate>Thu, 03 Dec 2009 21:41:00 +0000</pubDate>
		<dc:creator>诡异的西红柿</dc:creator>
				<category><![CDATA[数据结构]]></category>
		<category><![CDATA[趣题]]></category>

		<guid isPermaLink="false">/post/2009/12/03/Usage-of-LRU.aspx</guid>
		<description><![CDATA[前面有一篇文章里面我<a href="http://www.jguoer.com/post/2009/11/10/LRU-Introduce.aspx"><strong>写了LRU算法</strong></a>，其中还将一些算法更新了一下，现在我就做个笔记，写一下LRU算法的一些用途，另外说明一下，这里我们LRU算法都不会考虑空间不足的问题（因为从软件模拟来说基本不会存在这个问题），如果在CPU硬件设计上出现空间不足的话，我们直接将链表的第一个元素剔除即可，当然，也有相应硬件能够直接做这种事情。

当然，LRU不仅仅在硬件设计上有很好的作用，在软件设计上也有很好的作用，例如Orical的数据库就是用了LRU算法。我们可以先看看下面这个图。
<p style="text-align: center;"><img src="/blog/wp-content/uploads/2009/12/LRUandData.png" alt="" /></p>
Oracle的设计可以从上图看到，这里buffer（缓冲区）就像一个水池，水池的最小单位就是数据块。当每个数据块被读入buffer时，Oracle或相应的软件都会抽取数据块的头部，在内存中构建buffer header，并将这些buffer header串成链表的形式。而buffer header里面记录的指针就指向buffer中的该数据块本身。于是，当我们在搜索某个数据块时，就不用去缓冲区中找，而是直接扫描链表上该数据块所对应的buffer header，然后根据找到的buffer header所记录的指针就能到buffer中直接定位该数据块了。而维护buffer header就是hash表，而buffer header就是一个LRU维护的链表。

这里的buffer head就是数据的一些信息，例如如果我将数字存储到数据中，我可以通过一种方式获取其head的信息，存放到相应的hash组里面，而hash的key也是可以通过数据本身得到。例如我们可以将数字求100的余数得到key，然后余10得到页面id。例如25这个数字，我们可以余100为25，余10为5，那么我们就在缓冲区buffer[25,5]的地方将这个数据保存起来，下次我们取的时候可以直接从25获得hash的组id和存放的页面id，即25和5，则我们直接读取buffer[25,5]即可。

说了这么多，可以稍微总结一下，这个数据结构就是。
<ul>
	<li>hash表：存放数据的组，用于查找和维护。</li>
	<li>buffer head：LRU链表，被hash组维护，其内容为在内存（缓冲区）的页面id。</li>
	<li>buffer：缓冲区，用于存放数据。</li>
</ul>
简单的说，也就是当数据读入，我们通过一种方法获取组的id，然后在相应的组中维护一个页面id（链表），然后查找的时候直接通过组id和页面id得到数据，从而提高速度。

<strong>不过这里就会有一个问题</strong>，hash表这里就不存在冲突的问题了，因为hash表的value是一个链表结构，所以不存在冲突，如果有相同的key的话，我们直接在后面添加节点即可，但是在缓冲区的话就有可能有问题，例如25和125这两个值最后在缓冲区的位置应该是一样的，那么我们肯定不能覆盖掉，所以我们就查找25这个位置（25，5）后面的这个空间是否被使用（25，6），如果没被使用，则我们写到这个空间中，并更改LRU链表指针里面的值。

例如25会是第25组，其页id为5，当读到125的时候，发现也是25组和id为5，那么我们就查找6的位置，如果6位置没数据写入，那么OK，写到6的位置，并更新链表为第25组，页id为6，<strong>直到内存缓冲区被写满为止，我们都不覆盖</strong>。

这样我们解决了冲突问题，但是这样的话，查找也就出了问题，即不能够直接查找，同样是25和125这两个数，两个坐标最后会成为（25，5）和（25，6），但是我们两个数通过规则获得到的key和页面id是25和5。所以当我们查找的时候就不需要使用页面id，而是采用扫描的方式。<strong>即当我们搜索125这个数字的时候，可以获得组id，即25，然后扫描这个组维护的LRU链表，然后扫描内存块，查找是否是我们需要的数据，如果是，则返回。</strong>就是我们搜索125，发现组id为25，那么我们就找（25，5），发现数据为25，不是我们想要的，继续下一个链，（25，6）直到找到需要的数据。

不过上面我们也可以优化一下，即优化成结构体，每个数据都有自己的独立的页面id，而不是通过一种规则去找页面id，尽量避免冲突，提高效率。我们可以看一下Orical的做法。

“当前台进程发出SELECT或者其他DML语句时，Oracle根据SQL语句的执行计划找到符合SQL条件的数据块，然后Oracle会根据对请求的数据块的地址以及数据块的类型作为参数，应用hash函数以后，得到要找的数据块所处的hash bucket，也就是确定该数据块在哪条hash chain上。然后，Oracle进入该hash chain，从上面所挂的第一个buffer header开始，根据buffer header所含有的指针找到对应的块体，然后扫描其中的数据，确认其是否是SQL语句所需要的块，如果是，则返回该块里所需要的数据；否则，如果不是，则继续往下搜索，一直搜索到最后一个buffer header为止。如果一直都没有找到，则调用物理I/O，到数据文件里把该块所含有的内容复制一份到一个可用的buffer里，并构建该块的 buffer header，然后将该buffer header挂到hash chain上去。”

所以基本上我们可以明确算法了，下面我给一下C#模拟的代码。
<div class="code">
<div><img src="/fckeditor/editor/plugins/insertcode/images/None.gif" alt="" align="top" /><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> LRUandData
<img id="Highlighter_21_3347_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_21_3347_Open_Text').style.display='none'; document.getElementById('Highlighter_21_3347_Closed_Image').style.display='inline'; document.getElementById('Highlighter_21_3347_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><img id="Highlighter_21_3347_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_21_3347_Closed_Text').style.display='none'; document.getElementById('Highlighter_21_3347_Open_Image').style.display='inline'; document.getElementById('Highlighter_21_3347_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /></span><span id="Highlighter_21_3347_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_21_3347_Open_Text"><span style="color: #000000;">{
<img id="Highlighter_27_153_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_27_153_Closed_Text').style.display='none'; document.getElementById('Highlighter_27_153_Open_Image').style.display='inline'; document.getElementById('Highlighter_27_153_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_27_153_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_27_153_Open_Text').style.display='none'; document.getElementById('Highlighter_27_153_Closed_Image').style.display='inline'; document.getElementById('Highlighter_27_153_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_27_153_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">定义LRU链表</span><span id="Highlighter_27_153_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 定义LRU链表</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LRUList
<img id="Highlighter_73_137_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_73_137_Open_Text').style.display='none'; document.getElementById('Highlighter_73_137_Closed_Image').style.display='inline'; document.getElementById('Highlighter_73_137_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_73_137_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_73_137_Closed_Text').style.display='none'; document.getElementById('Highlighter_73_137_Open_Image').style.display='inline'; document.getElementById('Highlighter_73_137_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_73_137_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_73_137_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> pageNum;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> LRUList next;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
<img id="Highlighter_177_3345_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_177_3345_Open_Text').style.display='none'; document.getElementById('Highlighter_177_3345_Closed_Image').style.display='inline'; document.getElementById('Highlighter_177_3345_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_177_3345_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_177_3345_Closed_Text').style.display='none'; document.getElementById('Highlighter_177_3345_Open_Image').style.display='inline'; document.getElementById('Highlighter_177_3345_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_177_3345_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_177_3345_Open_Text"><span style="color: #000000;">{
<img id="Highlighter_187_417_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_187_417_Closed_Text').style.display='none'; document.getElementById('Highlighter_187_417_Open_Image').style.display='inline'; document.getElementById('Highlighter_187_417_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_187_417_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_187_417_Open_Text').style.display='none'; document.getElementById('Highlighter_187_417_Closed_Image').style.display='inline'; document.getElementById('Highlighter_187_417_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_187_417_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">定义数据块，缓冲区和hash表</span><span id="Highlighter_187_417_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 定义数据块，缓冲区和hash表</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img id="Highlighter_243_300_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_243_300_Open_Text').style.display='none'; document.getElementById('Highlighter_243_300_Closed_Image').style.display='inline'; document.getElementById('Highlighter_243_300_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_243_300_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_243_300_Closed_Text').style.display='none'; document.getElementById('Highlighter_243_300_Open_Image').style.display='inline'; document.getElementById('Highlighter_243_300_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[] numbers </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span id="Highlighter_243_300_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_243_300_Open_Text"><span style="color: #000000;">{ </span><span style="color: #800080;">100</span><span style="color: #000000;">, </span><span style="color: #800080;">25</span><span style="color: #000000;">, </span><span style="color: #800080;">64</span><span style="color: #000000;">, </span><span style="color: #800080;">1001</span><span style="color: #000000;">, </span><span style="color: #800080;">98</span><span style="color: #000000;">, </span><span style="color: #800080;">52</span><span style="color: #000000;">, </span><span style="color: #800080;">76</span><span style="color: #000000;">, </span><span style="color: #800080;">33</span><span style="color: #000000;">, </span><span style="color: #800080;">511</span><span style="color: #000000;">, </span><span style="color: #800080;">72</span><span style="color: #000000;">, </span><span style="color: #800080;">633</span><span style="color: #000000;">, </span><span style="color: #800080;">1000</span><span style="color: #000000;"> }</span></span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[,] buffer </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[</span><span style="color: #800080;">100</span><span style="color: #000000;">, </span><span style="color: #800080;">100</span><span style="color: #000000;">];
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> Dictionary</span><span style="color: #000000;">&#60;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">, LRUList</span><span style="color: #000000;">&#62;</span><span style="color: #000000;"> hash;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Main(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
<img id="Highlighter_467_703_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_467_703_Open_Text').style.display='none'; document.getElementById('Highlighter_467_703_Closed_Image').style.display='inline'; document.getElementById('Highlighter_467_703_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_467_703_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_467_703_Closed_Text').style.display='none'; document.getElementById('Highlighter_467_703_Open_Image').style.display='inline'; document.getElementById('Highlighter_467_703_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_467_703_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_467_703_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> hash </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Dictionary</span><span style="color: #000000;">&#60;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">, LRUList</span><span style="color: #000000;">&#62;</span><span style="color: #000000;">();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">初始化数据</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> InsertData();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">查询数据</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> value </span><span style="color: #000000;">=</span><span style="color: #000000;"> SearchData(</span><span style="color: #800080;">33</span><span style="color: #000000;">);
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Console.WriteLine(value);
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Console.ReadKey();
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img id="Highlighter_713_1885_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_713_1885_Closed_Text').style.display='none'; document.getElementById('Highlighter_713_1885_Open_Image').style.display='inline'; document.getElementById('Highlighter_713_1885_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_713_1885_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_713_1885_Open_Text').style.display='none'; document.getElementById('Highlighter_713_1885_Closed_Image').style.display='inline'; document.getElementById('Highlighter_713_1885_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_713_1885_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">初始化数据</span><span id="Highlighter_713_1885_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 初始化数据</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> InsertData()
<img id="Highlighter_769_1865_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_769_1865_Open_Text').style.display='none'; document.getElementById('Highlighter_769_1865_Closed_Image').style.display='inline'; document.getElementById('Highlighter_769_1865_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_769_1865_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_769_1865_Closed_Text').style.display='none'; document.getElementById('Highlighter_769_1865_Open_Image').style.display='inline'; document.getElementById('Highlighter_769_1865_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_769_1865_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_769_1865_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">key的算法是求该数和100的余数
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">pageNum的算法是求该数和10的余数</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;">&#60;</span><span style="color: #000000;"> numbers.Length; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)
<img id="Highlighter_903_1855_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_903_1855_Open_Text').style.display='none'; document.getElementById('Highlighter_903_1855_Closed_Image').style.display='inline'; document.getElementById('Highlighter_903_1855_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_903_1855_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_903_1855_Closed_Text').style.display='none'; document.getElementById('Highlighter_903_1855_Open_Image').style.display='inline'; document.getElementById('Highlighter_903_1855_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_903_1855_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_903_1855_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> key </span><span style="color: #000000;">=</span><span style="color: #000000;"> numbers[i] </span><span style="color: #000000;">%</span><span style="color: #000000;"> </span><span style="color: #800080;">100</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> LRUList lrulist </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LRUList();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> lrulist.pageNum </span><span style="color: #000000;">=</span><span style="color: #000000;"> numbers[i] </span><span style="color: #000000;">%</span><span style="color: #000000;"> </span><span style="color: #800080;">10</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> lrulist.next </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> pageId </span><span style="color: #000000;">=</span><span style="color: #000000;"> lrulist.pageNum;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果存在这个组则添加链表到尾部</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (hash.ContainsKey(key))
<img id="Highlighter_1226_1662_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1226_1662_Open_Text').style.display='none'; document.getElementById('Highlighter_1226_1662_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1226_1662_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1226_1662_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1226_1662_Closed_Text').style.display='none'; document.getElementById('Highlighter_1226_1662_Open_Image').style.display='inline'; document.getElementById('Highlighter_1226_1662_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1226_1662_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1226_1662_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> LRUList templist </span><span style="color: #000000;">=</span><span style="color: #000000;"> hash[key];
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (templist.next </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">)
<img id="Highlighter_1348_1421_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1348_1421_Open_Text').style.display='none'; document.getElementById('Highlighter_1348_1421_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1348_1421_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1348_1421_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1348_1421_Closed_Text').style.display='none'; document.getElementById('Highlighter_1348_1421_Open_Image').style.display='inline'; document.getElementById('Highlighter_1348_1421_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1348_1421_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1348_1421_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> templist </span><span style="color: #000000;">=</span><span style="color: #000000;"> templist.next;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (buffer[key, pageId] </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)
<img id="Highlighter_1496_1553_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1496_1553_Open_Text').style.display='none'; document.getElementById('Highlighter_1496_1553_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1496_1553_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1496_1553_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1496_1553_Closed_Text').style.display='none'; document.getElementById('Highlighter_1496_1553_Open_Image').style.display='inline'; document.getElementById('Highlighter_1496_1553_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1496_1553_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1496_1553_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> pageId</span><span style="color: #000000;">++</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> lrulist.pageNum </span><span style="color: #000000;">=</span><span style="color: #000000;"> pageId;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> templist.next </span><span style="color: #000000;">=</span><span style="color: #000000;"> lrulist;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
<img id="Highlighter_1700_1791_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1700_1791_Open_Text').style.display='none'; document.getElementById('Highlighter_1700_1791_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1700_1791_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1700_1791_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1700_1791_Closed_Text').style.display='none'; document.getElementById('Highlighter_1700_1791_Open_Image').style.display='inline'; document.getElementById('Highlighter_1700_1791_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1700_1791_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1700_1791_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">创建一个组</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> hash.Add(key, lrulist);
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> buffer[key, pageId] </span><span style="color: #000000;">=</span><span style="color: #000000;"> numbers[i];
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img id="Highlighter_1895_3339_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1895_3339_Closed_Text').style.display='none'; document.getElementById('Highlighter_1895_3339_Open_Image').style.display='inline'; document.getElementById('Highlighter_1895_3339_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_1895_3339_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1895_3339_Open_Text').style.display='none'; document.getElementById('Highlighter_1895_3339_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1895_3339_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_1895_3339_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">查找数据</span><span id="Highlighter_1895_3339_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 查找数据</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> SearchData(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> number)
<img id="Highlighter_1959_3319_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1959_3319_Open_Text').style.display='none'; document.getElementById('Highlighter_1959_3319_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1959_3319_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1959_3319_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1959_3319_Closed_Text').style.display='none'; document.getElementById('Highlighter_1959_3319_Open_Image').style.display='inline'; document.getElementById('Highlighter_1959_3319_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1959_3319_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1959_3319_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> key </span><span style="color: #000000;">=</span><span style="color: #000000;"> number </span><span style="color: #000000;">%</span><span style="color: #000000;"> </span><span style="color: #800080;">100</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> retValue </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">-</span><span style="color: #800080;">1</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (hash.ContainsKey(key))
<img id="Highlighter_2080_3279_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2080_3279_Open_Text').style.display='none'; document.getElementById('Highlighter_2080_3279_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2080_3279_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2080_3279_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2080_3279_Closed_Text').style.display='none'; document.getElementById('Highlighter_2080_3279_Open_Image').style.display='inline'; document.getElementById('Highlighter_2080_3279_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2080_3279_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_2080_3279_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> LRUList list </span><span style="color: #000000;">=</span><span style="color: #000000;"> hash[key];
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> LRUList templist </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LRUList();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> LRUList slist </span><span style="color: #000000;">=</span><span style="color: #000000;"> list;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">LRU算法，算第一个节点的值</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (number </span><span style="color: #000000;">==</span><span style="color: #000000;"> buffer[key, list.pageNum])
<img id="Highlighter_2318_2501_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2318_2501_Open_Text').style.display='none'; document.getElementById('Highlighter_2318_2501_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2318_2501_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2318_2501_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2318_2501_Closed_Text').style.display='none'; document.getElementById('Highlighter_2318_2501_Open_Image').style.display='inline'; document.getElementById('Highlighter_2318_2501_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2318_2501_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_2318_2501_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> templist.pageNum </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.pageNum;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> list </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.next;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> slist </span><span style="color: #000000;">=</span><span style="color: #000000;"> list;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> retValue </span><span style="color: #000000;">=</span><span style="color: #000000;"> number;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">不是第一个节点的值则遍历后面的节点</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (list.next </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">)
<img id="Highlighter_2596_2964_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2596_2964_Open_Text').style.display='none'; document.getElementById('Highlighter_2596_2964_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2596_2964_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2596_2964_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2596_2964_Closed_Text').style.display='none'; document.getElementById('Highlighter_2596_2964_Open_Image').style.display='inline'; document.getElementById('Highlighter_2596_2964_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2596_2964_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_2596_2964_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (number </span><span style="color: #000000;">==</span><span style="color: #000000;"> buffer[key, list.pageNum])
<img id="Highlighter_2679_2908_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2679_2908_Open_Text').style.display='none'; document.getElementById('Highlighter_2679_2908_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2679_2908_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2679_2908_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2679_2908_Closed_Text').style.display='none'; document.getElementById('Highlighter_2679_2908_Open_Image').style.display='inline'; document.getElementById('Highlighter_2679_2908_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2679_2908_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_2679_2908_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> templist </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.next;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> templist.next </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> list.next </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.next.next;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> retValue </span><span style="color: #000000;">=</span><span style="color: #000000;"> buffer[key, list.pageNum];
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> list </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.next;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> list.next </span><span style="color: #000000;">=</span><span style="color: #000000;"> templist;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果是尾部节点的值
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">LRU链表有几个值得注意的地方，即第0个节点和最后节点的置换</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (number </span><span style="color: #000000;">==</span><span style="color: #000000;"> buffer[key, list.pageNum])
<img id="Highlighter_3153_3230_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_3153_3230_Open_Text').style.display='none'; document.getElementById('Highlighter_3153_3230_Closed_Image').style.display='inline'; document.getElementById('Highlighter_3153_3230_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_3153_3230_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_3153_3230_Closed_Text').style.display='none'; document.getElementById('Highlighter_3153_3230_Open_Image').style.display='inline'; document.getElementById('Highlighter_3153_3230_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_3153_3230_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_3153_3230_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> retValue </span><span style="color: #000000;">=</span><span style="color: #000000;"> buffer[key, list.pageNum];
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> hash[key] </span><span style="color: #000000;">=</span><span style="color: #000000;"> slist;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> retValue;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/None.gif" alt="" align="top" /></span></div>
</div>
上面的代码模拟了基本的操作，如果有更多功能只能大家自己改了。：）

从这里我们也可以看到，设计一个好的软件，数学，数据结构，计算机组成原理等等基础的东西是非常重要的，决定技术高度，基础的牢固程度最重要，举个老师给我们举了一百遍的例子，房子的高度由地基牢固程度决定，真的一点不假。]]></description>
			<content:encoded><![CDATA[<p>前面有一篇文章里面我<a href="http://www.jguoer.com/post/2009/11/10/LRU-Introduce.aspx"><strong>写了LRU算法</strong></a>，其中还将一些算法更新了一下，现在我就做个笔记，写一下LRU算法的一些用途，另外说明一下，这里我们LRU算法都不会考虑空间不足的问题（因为从软件模拟来说基本不会存在这个问题），如果在CPU硬件设计上出现空间不足的话，我们直接将链表的第一个元素剔除即可，当然，也有相应硬件能够直接做这种事情。</p>
<p>当然，LRU不仅仅在硬件设计上有很好的作用，在软件设计上也有很好的作用，例如Orical的数据库就是用了LRU算法。我们可以先看看下面这个图。</p>
<p style="text-align: center;"><img src="/blog/wp-content/uploads/2009/12/LRUandData.png" alt="" /></p>
<p>Oracle的设计可以从上图看到，这里buffer（缓冲区）就像一个水池，水池的最小单位就是数据块。当每个数据块被读入buffer时，Oracle或相应的软件都会抽取数据块的头部，在内存中构建buffer header，并将这些buffer header串成链表的形式。而buffer header里面记录的指针就指向buffer中的该数据块本身。于是，当我们在搜索某个数据块时，就不用去缓冲区中找，而是直接扫描链表上该数据块所对应的buffer header，然后根据找到的buffer header所记录的指针就能到buffer中直接定位该数据块了。而维护buffer header就是hash表，而buffer header就是一个LRU维护的链表。</p>
<p>这里的buffer head就是数据的一些信息，例如如果我将数字存储到数据中，我可以通过一种方式获取其head的信息，存放到相应的hash组里面，而hash的key也是可以通过数据本身得到。例如我们可以将数字求100的余数得到key，然后余10得到页面id。例如25这个数字，我们可以余100为25，余10为5，那么我们就在缓冲区buffer[25,5]的地方将这个数据保存起来，下次我们取的时候可以直接从25获得hash的组id和存放的页面id，即25和5，则我们直接读取buffer[25,5]即可。</p>
<p>说了这么多，可以稍微总结一下，这个数据结构就是。</p>
<ul>
<li>hash表：存放数据的组，用于查找和维护。</li>
<li>buffer head：LRU链表，被hash组维护，其内容为在内存（缓冲区）的页面id。</li>
<li>buffer：缓冲区，用于存放数据。</li>
</ul>
<p>简单的说，也就是当数据读入，我们通过一种方法获取组的id，然后在相应的组中维护一个页面id（链表），然后查找的时候直接通过组id和页面id得到数据，从而提高速度。</p>
<p><strong>不过这里就会有一个问题</strong>，hash表这里就不存在冲突的问题了，因为hash表的value是一个链表结构，所以不存在冲突，如果有相同的key的话，我们直接在后面添加节点即可，但是在缓冲区的话就有可能有问题，例如25和125这两个值最后在缓冲区的位置应该是一样的，那么我们肯定不能覆盖掉，所以我们就查找25这个位置（25，5）后面的这个空间是否被使用（25，6），如果没被使用，则我们写到这个空间中，并更改LRU链表指针里面的值。</p>
<p>例如25会是第25组，其页id为5，当读到125的时候，发现也是25组和id为5，那么我们就查找6的位置，如果6位置没数据写入，那么OK，写到6的位置，并更新链表为第25组，页id为6，<strong>直到内存缓冲区被写满为止，我们都不覆盖</strong>。</p>
<p>这样我们解决了冲突问题，但是这样的话，查找也就出了问题，即不能够直接查找，同样是25和125这两个数，两个坐标最后会成为（25，5）和（25，6），但是我们两个数通过规则获得到的key和页面id是25和5。所以当我们查找的时候就不需要使用页面id，而是采用扫描的方式。<strong>即当我们搜索125这个数字的时候，可以获得组id，即25，然后扫描这个组维护的LRU链表，然后扫描内存块，查找是否是我们需要的数据，如果是，则返回。</strong>就是我们搜索125，发现组id为25，那么我们就找（25，5），发现数据为25，不是我们想要的，继续下一个链，（25，6）直到找到需要的数据。</p>
<p>不过上面我们也可以优化一下，即优化成结构体，每个数据都有自己的独立的页面id，而不是通过一种规则去找页面id，尽量避免冲突，提高效率。我们可以看一下Orical的做法。</p>
<p>“当前台进程发出SELECT或者其他DML语句时，Oracle根据SQL语句的执行计划找到符合SQL条件的数据块，然后Oracle会根据对请求的数据块的地址以及数据块的类型作为参数，应用hash函数以后，得到要找的数据块所处的hash bucket，也就是确定该数据块在哪条hash chain上。然后，Oracle进入该hash chain，从上面所挂的第一个buffer header开始，根据buffer header所含有的指针找到对应的块体，然后扫描其中的数据，确认其是否是SQL语句所需要的块，如果是，则返回该块里所需要的数据；否则，如果不是，则继续往下搜索，一直搜索到最后一个buffer header为止。如果一直都没有找到，则调用物理I/O，到数据文件里把该块所含有的内容复制一份到一个可用的buffer里，并构建该块的 buffer header，然后将该buffer header挂到hash chain上去。”</p>
<p>所以基本上我们可以明确算法了，下面我给一下C#模拟的代码。</p>
<div class="code">
<div><img src="/fckeditor/editor/plugins/insertcode/images/None.gif" alt="" align="top" /><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> LRUandData<br />
<img id="Highlighter_21_3347_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_21_3347_Open_Text').style.display='none'; document.getElementById('Highlighter_21_3347_Closed_Image').style.display='inline'; document.getElementById('Highlighter_21_3347_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><img id="Highlighter_21_3347_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_21_3347_Closed_Text').style.display='none'; document.getElementById('Highlighter_21_3347_Open_Image').style.display='inline'; document.getElementById('Highlighter_21_3347_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /></span><span id="Highlighter_21_3347_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_21_3347_Open_Text"><span style="color: #000000;">{<br />
<img id="Highlighter_27_153_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_27_153_Closed_Text').style.display='none'; document.getElementById('Highlighter_27_153_Open_Image').style.display='inline'; document.getElementById('Highlighter_27_153_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_27_153_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_27_153_Open_Text').style.display='none'; document.getElementById('Highlighter_27_153_Closed_Image').style.display='inline'; document.getElementById('Highlighter_27_153_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_27_153_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">定义LRU链表</span><span id="Highlighter_27_153_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 定义LRU链表</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> LRUList<br />
<img id="Highlighter_73_137_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_73_137_Open_Text').style.display='none'; document.getElementById('Highlighter_73_137_Closed_Image').style.display='inline'; document.getElementById('Highlighter_73_137_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_73_137_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_73_137_Closed_Text').style.display='none'; document.getElementById('Highlighter_73_137_Open_Image').style.display='inline'; document.getElementById('Highlighter_73_137_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_73_137_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_73_137_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> pageNum;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> LRUList next;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program<br />
<img id="Highlighter_177_3345_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_177_3345_Open_Text').style.display='none'; document.getElementById('Highlighter_177_3345_Closed_Image').style.display='inline'; document.getElementById('Highlighter_177_3345_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_177_3345_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_177_3345_Closed_Text').style.display='none'; document.getElementById('Highlighter_177_3345_Open_Image').style.display='inline'; document.getElementById('Highlighter_177_3345_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_177_3345_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_177_3345_Open_Text"><span style="color: #000000;">{<br />
<img id="Highlighter_187_417_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_187_417_Closed_Text').style.display='none'; document.getElementById('Highlighter_187_417_Open_Image').style.display='inline'; document.getElementById('Highlighter_187_417_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_187_417_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_187_417_Open_Text').style.display='none'; document.getElementById('Highlighter_187_417_Closed_Image').style.display='inline'; document.getElementById('Highlighter_187_417_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_187_417_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">定义数据块，缓冲区和hash表</span><span id="Highlighter_187_417_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 定义数据块，缓冲区和hash表</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img id="Highlighter_243_300_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_243_300_Open_Text').style.display='none'; document.getElementById('Highlighter_243_300_Closed_Image').style.display='inline'; document.getElementById('Highlighter_243_300_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_243_300_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_243_300_Closed_Text').style.display='none'; document.getElementById('Highlighter_243_300_Open_Image').style.display='inline'; document.getElementById('Highlighter_243_300_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[] numbers </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span id="Highlighter_243_300_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_243_300_Open_Text"><span style="color: #000000;">{ </span><span style="color: #800080;">100</span><span style="color: #000000;">, </span><span style="color: #800080;">25</span><span style="color: #000000;">, </span><span style="color: #800080;">64</span><span style="color: #000000;">, </span><span style="color: #800080;">1001</span><span style="color: #000000;">, </span><span style="color: #800080;">98</span><span style="color: #000000;">, </span><span style="color: #800080;">52</span><span style="color: #000000;">, </span><span style="color: #800080;">76</span><span style="color: #000000;">, </span><span style="color: #800080;">33</span><span style="color: #000000;">, </span><span style="color: #800080;">511</span><span style="color: #000000;">, </span><span style="color: #800080;">72</span><span style="color: #000000;">, </span><span style="color: #800080;">633</span><span style="color: #000000;">, </span><span style="color: #800080;">1000</span><span style="color: #000000;"> }</span></span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[,] buffer </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[</span><span style="color: #800080;">100</span><span style="color: #000000;">, </span><span style="color: #800080;">100</span><span style="color: #000000;">];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> Dictionary</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">, LRUList</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> hash;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Main(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)<br />
<img id="Highlighter_467_703_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_467_703_Open_Text').style.display='none'; document.getElementById('Highlighter_467_703_Closed_Image').style.display='inline'; document.getElementById('Highlighter_467_703_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_467_703_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_467_703_Closed_Text').style.display='none'; document.getElementById('Highlighter_467_703_Open_Image').style.display='inline'; document.getElementById('Highlighter_467_703_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_467_703_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_467_703_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> hash </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Dictionary</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">, LRUList</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">初始化数据</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> InsertData();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">查询数据</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> value </span><span style="color: #000000;">=</span><span style="color: #000000;"> SearchData(</span><span style="color: #800080;">33</span><span style="color: #000000;">);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Console.WriteLine(value);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> Console.ReadKey();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img id="Highlighter_713_1885_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_713_1885_Closed_Text').style.display='none'; document.getElementById('Highlighter_713_1885_Open_Image').style.display='inline'; document.getElementById('Highlighter_713_1885_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_713_1885_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_713_1885_Open_Text').style.display='none'; document.getElementById('Highlighter_713_1885_Closed_Image').style.display='inline'; document.getElementById('Highlighter_713_1885_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_713_1885_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">初始化数据</span><span id="Highlighter_713_1885_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 初始化数据</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> InsertData()<br />
<img id="Highlighter_769_1865_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_769_1865_Open_Text').style.display='none'; document.getElementById('Highlighter_769_1865_Closed_Image').style.display='inline'; document.getElementById('Highlighter_769_1865_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_769_1865_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_769_1865_Closed_Text').style.display='none'; document.getElementById('Highlighter_769_1865_Open_Image').style.display='inline'; document.getElementById('Highlighter_769_1865_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_769_1865_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_769_1865_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">key的算法是求该数和100的余数<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">pageNum的算法是求该数和10的余数</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> numbers.Length; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />
<img id="Highlighter_903_1855_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_903_1855_Open_Text').style.display='none'; document.getElementById('Highlighter_903_1855_Closed_Image').style.display='inline'; document.getElementById('Highlighter_903_1855_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_903_1855_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_903_1855_Closed_Text').style.display='none'; document.getElementById('Highlighter_903_1855_Open_Image').style.display='inline'; document.getElementById('Highlighter_903_1855_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_903_1855_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_903_1855_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> key </span><span style="color: #000000;">=</span><span style="color: #000000;"> numbers[i] </span><span style="color: #000000;">%</span><span style="color: #000000;"> </span><span style="color: #800080;">100</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> LRUList lrulist </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LRUList();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> lrulist.pageNum </span><span style="color: #000000;">=</span><span style="color: #000000;"> numbers[i] </span><span style="color: #000000;">%</span><span style="color: #000000;"> </span><span style="color: #800080;">10</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> lrulist.next </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> pageId </span><span style="color: #000000;">=</span><span style="color: #000000;"> lrulist.pageNum;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果存在这个组则添加链表到尾部</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (hash.ContainsKey(key))<br />
<img id="Highlighter_1226_1662_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1226_1662_Open_Text').style.display='none'; document.getElementById('Highlighter_1226_1662_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1226_1662_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1226_1662_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1226_1662_Closed_Text').style.display='none'; document.getElementById('Highlighter_1226_1662_Open_Image').style.display='inline'; document.getElementById('Highlighter_1226_1662_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1226_1662_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1226_1662_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> LRUList templist </span><span style="color: #000000;">=</span><span style="color: #000000;"> hash[key];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (templist.next </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">)<br />
<img id="Highlighter_1348_1421_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1348_1421_Open_Text').style.display='none'; document.getElementById('Highlighter_1348_1421_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1348_1421_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1348_1421_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1348_1421_Closed_Text').style.display='none'; document.getElementById('Highlighter_1348_1421_Open_Image').style.display='inline'; document.getElementById('Highlighter_1348_1421_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1348_1421_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1348_1421_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> templist </span><span style="color: #000000;">=</span><span style="color: #000000;"> templist.next;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (buffer[key, pageId] </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)<br />
<img id="Highlighter_1496_1553_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1496_1553_Open_Text').style.display='none'; document.getElementById('Highlighter_1496_1553_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1496_1553_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1496_1553_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1496_1553_Closed_Text').style.display='none'; document.getElementById('Highlighter_1496_1553_Open_Image').style.display='inline'; document.getElementById('Highlighter_1496_1553_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1496_1553_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1496_1553_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> pageId</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> lrulist.pageNum </span><span style="color: #000000;">=</span><span style="color: #000000;"> pageId;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> templist.next </span><span style="color: #000000;">=</span><span style="color: #000000;"> lrulist;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br />
<img id="Highlighter_1700_1791_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1700_1791_Open_Text').style.display='none'; document.getElementById('Highlighter_1700_1791_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1700_1791_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1700_1791_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1700_1791_Closed_Text').style.display='none'; document.getElementById('Highlighter_1700_1791_Open_Image').style.display='inline'; document.getElementById('Highlighter_1700_1791_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1700_1791_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1700_1791_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">创建一个组</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> hash.Add(key, lrulist);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> buffer[key, pageId] </span><span style="color: #000000;">=</span><span style="color: #000000;"> numbers[i];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img id="Highlighter_1895_3339_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1895_3339_Closed_Text').style.display='none'; document.getElementById('Highlighter_1895_3339_Open_Image').style.display='inline'; document.getElementById('Highlighter_1895_3339_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_1895_3339_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1895_3339_Open_Text').style.display='none'; document.getElementById('Highlighter_1895_3339_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1895_3339_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /> </span><span id="Highlighter_1895_3339_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">查找数据</span><span id="Highlighter_1895_3339_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 查找数据</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> SearchData(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> number)<br />
<img id="Highlighter_1959_3319_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1959_3319_Open_Text').style.display='none'; document.getElementById('Highlighter_1959_3319_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1959_3319_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1959_3319_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1959_3319_Closed_Text').style.display='none'; document.getElementById('Highlighter_1959_3319_Open_Image').style.display='inline'; document.getElementById('Highlighter_1959_3319_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1959_3319_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1959_3319_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> key </span><span style="color: #000000;">=</span><span style="color: #000000;"> number </span><span style="color: #000000;">%</span><span style="color: #000000;"> </span><span style="color: #800080;">100</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> retValue </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">-</span><span style="color: #800080;">1</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (hash.ContainsKey(key))<br />
<img id="Highlighter_2080_3279_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2080_3279_Open_Text').style.display='none'; document.getElementById('Highlighter_2080_3279_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2080_3279_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2080_3279_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2080_3279_Closed_Text').style.display='none'; document.getElementById('Highlighter_2080_3279_Open_Image').style.display='inline'; document.getElementById('Highlighter_2080_3279_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2080_3279_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2080_3279_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> LRUList list </span><span style="color: #000000;">=</span><span style="color: #000000;"> hash[key];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> LRUList templist </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> LRUList();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> LRUList slist </span><span style="color: #000000;">=</span><span style="color: #000000;"> list;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">LRU算法，算第一个节点的值</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (number </span><span style="color: #000000;">==</span><span style="color: #000000;"> buffer[key, list.pageNum])<br />
<img id="Highlighter_2318_2501_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2318_2501_Open_Text').style.display='none'; document.getElementById('Highlighter_2318_2501_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2318_2501_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2318_2501_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2318_2501_Closed_Text').style.display='none'; document.getElementById('Highlighter_2318_2501_Open_Image').style.display='inline'; document.getElementById('Highlighter_2318_2501_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2318_2501_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2318_2501_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> templist.pageNum </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.pageNum;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> list </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.next;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> slist </span><span style="color: #000000;">=</span><span style="color: #000000;"> list;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> retValue </span><span style="color: #000000;">=</span><span style="color: #000000;"> number;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">不是第一个节点的值则遍历后面的节点</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (list.next </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">)<br />
<img id="Highlighter_2596_2964_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2596_2964_Open_Text').style.display='none'; document.getElementById('Highlighter_2596_2964_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2596_2964_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2596_2964_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2596_2964_Closed_Text').style.display='none'; document.getElementById('Highlighter_2596_2964_Open_Image').style.display='inline'; document.getElementById('Highlighter_2596_2964_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2596_2964_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2596_2964_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (number </span><span style="color: #000000;">==</span><span style="color: #000000;"> buffer[key, list.pageNum])<br />
<img id="Highlighter_2679_2908_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2679_2908_Open_Text').style.display='none'; document.getElementById('Highlighter_2679_2908_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2679_2908_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2679_2908_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2679_2908_Closed_Text').style.display='none'; document.getElementById('Highlighter_2679_2908_Open_Image').style.display='inline'; document.getElementById('Highlighter_2679_2908_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2679_2908_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2679_2908_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> templist </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.next;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> templist.next </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> list.next </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.next.next;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> retValue </span><span style="color: #000000;">=</span><span style="color: #000000;"> buffer[key, list.pageNum];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> list </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.next;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> list.next </span><span style="color: #000000;">=</span><span style="color: #000000;"> templist;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果是尾部节点的值<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">LRU链表有几个值得注意的地方，即第0个节点和最后节点的置换</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (number </span><span style="color: #000000;">==</span><span style="color: #000000;"> buffer[key, list.pageNum])<br />
<img id="Highlighter_3153_3230_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_3153_3230_Open_Text').style.display='none'; document.getElementById('Highlighter_3153_3230_Closed_Image').style.display='inline'; document.getElementById('Highlighter_3153_3230_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_3153_3230_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_3153_3230_Closed_Text').style.display='none'; document.getElementById('Highlighter_3153_3230_Open_Image').style.display='inline'; document.getElementById('Highlighter_3153_3230_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_3153_3230_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_3153_3230_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> retValue </span><span style="color: #000000;">=</span><span style="color: #000000;"> buffer[key, list.pageNum];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> hash[key] </span><span style="color: #000000;">=</span><span style="color: #000000;"> slist;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> retValue;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> </span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/None.gif" alt="" align="top" /></span></div>
</div>
<p>上面的代码模拟了基本的操作，如果有更多功能只能大家自己改了。：）</p>
<p>从这里我们也可以看到，设计一个好的软件，数学，数据结构，计算机组成原理等等基础的东西是非常重要的，决定技术高度，基础的牢固程度最重要，举个老师给我们举了一百遍的例子，房子的高度由地基牢固程度决定，真的一点不假。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jguoer.com/blog/index.php/archives/217/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>一道生成树的题</title>
		<link>http://www.jguoer.com/blog/index.php/archives/221</link>
		<comments>http://www.jguoer.com/blog/index.php/archives/221#comments</comments>
		<pubDate>Thu, 26 Nov 2009 21:58:00 +0000</pubDate>
		<dc:creator>诡异的西红柿</dc:creator>
				<category><![CDATA[数据结构]]></category>
		<category><![CDATA[趣题]]></category>

		<guid isPermaLink="false">/post/2009/11/26/Tree-Generator.aspx</guid>
		<description><![CDATA[昨天同事说了一道题，这道题说难也难，说简单也简单，但是细节比较麻烦，是个难题，如果这个是面试题的话，确实能够拦下很多人。

题目：有一串字符串，根据字符串生成二叉树，输入输出如下（输出是以树的形式输出）。

输入：{1}

输出：1

输入：{1{2,3}}

输出：根节点1，1的左节点为2，1的右节点为3

输入：{1{2{4},3{5,6}}}

输出：根节点1，1的左节点为2，2的左节点为4，右节点为空，1的右节点为3，3的左右节点分别为5，6。

下面是代码，慎看，并不一定是最好的，与标答还是有一定的距离的，不过效率还不错，标答就比较难理解了，过一段时间我再把标答发上来。思路不是很难，但是代码估计很难看懂：（
<div class="code">
<div><img id="Highlighter_0_3522_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_0_3522_Closed_Text').style.display='none'; document.getElementById('Highlighter_0_3522_Open_Image').style.display='inline'; document.getElementById('Highlighter_0_3522_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_0_3522_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_0_3522_Open_Text').style.display='none'; document.getElementById('Highlighter_0_3522_Closed_Image').style.display='inline'; document.getElementById('Highlighter_0_3522_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><span id="Highlighter_0_3522_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">生成树</span><span id="Highlighter_0_3522_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 生成树</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img id="Highlighter_13_180_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_13_180_Closed_Text').style.display='none'; document.getElementById('Highlighter_13_180_Open_Image').style.display='inline'; document.getElementById('Highlighter_13_180_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_13_180_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_13_180_Open_Text').style.display='none'; document.getElementById('Highlighter_13_180_Closed_Image').style.display='inline'; document.getElementById('Highlighter_13_180_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /></span><span id="Highlighter_13_180_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">思路</span><span id="Highlighter_13_180_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 思路</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">我们遍历字符串并压入到堆里面，如{1{2{4},3}}
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">我们找到}字符的位置，然后此处到前面{符号的
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">字符都出栈，生成树节点，即生成了2和2的左节点
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">然后我们再进栈，直到3的位置，我们再出栈，同理
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">我们可以生成1，2，3这三个节点，即1左节点为2
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">1的右节点为3</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img id="Highlighter_182_3510_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_182_3510_Closed_Text').style.display='none'; document.getElementById('Highlighter_182_3510_Open_Image').style.display='inline'; document.getElementById('Highlighter_182_3510_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_182_3510_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_182_3510_Open_Text').style.display='none'; document.getElementById('Highlighter_182_3510_Closed_Image').style.display='inline'; document.getElementById('Highlighter_182_3510_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /></span><span id="Highlighter_182_3510_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Code</span><span id="Highlighter_182_3510_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Code</span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> TreeGenerator
<img id="Highlighter_220_3498_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_220_3498_Open_Text').style.display='none'; document.getElementById('Highlighter_220_3498_Closed_Image').style.display='inline'; document.getElementById('Highlighter_220_3498_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_220_3498_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_220_3498_Closed_Text').style.display='none'; document.getElementById('Highlighter_220_3498_Open_Image').style.display='inline'; document.getElementById('Highlighter_220_3498_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_220_3498_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_220_3498_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> TreeNode
<img id="Highlighter_245_342_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_245_342_Open_Text').style.display='none'; document.getElementById('Highlighter_245_342_Closed_Image').style.display='inline'; document.getElementById('Highlighter_245_342_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_245_342_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_245_342_Closed_Text').style.display='none'; document.getElementById('Highlighter_245_342_Open_Image').style.display='inline'; document.getElementById('Highlighter_245_342_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_245_342_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_245_342_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> data;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> TreeNode lchild;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> TreeNode rchild;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
<img id="Highlighter_366_3496_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_366_3496_Open_Text').style.display='none'; document.getElementById('Highlighter_366_3496_Closed_Image').style.display='inline'; document.getElementById('Highlighter_366_3496_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_366_3496_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_366_3496_Closed_Text').style.display='none'; document.getElementById('Highlighter_366_3496_Open_Image').style.display='inline'; document.getElementById('Highlighter_366_3496_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_366_3496_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_366_3496_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">一个存放字符串的栈</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> Stack</span><span style="color: #000000;">&#60;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&#62;</span><span style="color: #000000;"> stack;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">一个存放节点的栈</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> Stack</span><span style="color: #000000;">&#60;</span><span style="color: #000000;">TreeNode</span><span style="color: #000000;">&#62;</span><span style="color: #000000;"> stackTree;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Main(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
<img id="Highlighter_532_713_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_532_713_Open_Text').style.display='none'; document.getElementById('Highlighter_532_713_Closed_Image').style.display='inline'; document.getElementById('Highlighter_532_713_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_532_713_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_532_713_Closed_Text').style.display='none'; document.getElementById('Highlighter_532_713_Open_Image').style.display='inline'; document.getElementById('Highlighter_532_713_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_532_713_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_532_713_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stack </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Stack</span><span style="color: #000000;">&#60;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&#62;</span><span style="color: #000000;">();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stackTree </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Stack</span><span style="color: #000000;">&#60;</span><span style="color: #000000;">TreeNode</span><span style="color: #000000;">&#62;</span><span style="color: #000000;">();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> treecode </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">{1{2,3{2,2}}}</span><span style="color: #800000;">"</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> GenerateNodes(treecode);
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> GenerateNodes(</span><span style="color: #0000ff;">string</span><span style="color: #000000;"> treecode)
<img id="Highlighter_774_1356_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_774_1356_Open_Text').style.display='none'; document.getElementById('Highlighter_774_1356_Closed_Image').style.display='inline'; document.getElementById('Highlighter_774_1356_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_774_1356_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_774_1356_Closed_Text').style.display='none'; document.getElementById('Highlighter_774_1356_Open_Image').style.display='inline'; document.getElementById('Highlighter_774_1356_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_774_1356_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_774_1356_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果为{1}则直接生成退出</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (treecode.Length </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">3</span><span style="color: #000000;">)
<img id="Highlighter_854_1026_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_854_1026_Open_Text').style.display='none'; document.getElementById('Highlighter_854_1026_Closed_Image').style.display='inline'; document.getElementById('Highlighter_854_1026_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_854_1026_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_854_1026_Closed_Text').style.display='none'; document.getElementById('Highlighter_854_1026_Open_Image').style.display='inline'; document.getElementById('Highlighter_854_1026_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_854_1026_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_854_1026_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> TreeNode tpnode </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> TreeNode();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> tpnode.data </span><span style="color: #000000;">=</span><span style="color: #000000;"> treecode[</span><span style="color: #800080;">1</span><span style="color: #000000;">];
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stackTree.Push(tpnode);
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">否则遍历字符串</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;">&#60;</span><span style="color: #000000;"> treecode.Length; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)
<img id="Highlighter_1116_1346_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1116_1346_Open_Text').style.display='none'; document.getElementById('Highlighter_1116_1346_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1116_1346_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1116_1346_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1116_1346_Closed_Text').style.display='none'; document.getElementById('Highlighter_1116_1346_Open_Image').style.display='inline'; document.getElementById('Highlighter_1116_1346_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1116_1346_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1116_1346_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stack.Push(treecode[i]);
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果不是}就进栈</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (treecode[i] </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">}</span><span style="color: #800000;">'</span><span style="color: #000000;">)
<img id="Highlighter_1243_1332_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1243_1332_Open_Text').style.display='none'; document.getElementById('Highlighter_1243_1332_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1243_1332_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1243_1332_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1243_1332_Closed_Text').style.display='none'; document.getElementById('Highlighter_1243_1332_Open_Image').style.display='inline'; document.getElementById('Highlighter_1243_1332_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1243_1332_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1243_1332_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">否则就创建节点</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> PopAndCreateNode();
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> PopAndCreateNode()
<img id="Highlighter_1405_2472_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1405_2472_Open_Text').style.display='none'; document.getElementById('Highlighter_1405_2472_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1405_2472_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1405_2472_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1405_2472_Closed_Text').style.display='none'; document.getElementById('Highlighter_1405_2472_Open_Image').style.display='inline'; document.getElementById('Highlighter_1405_2472_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1405_2472_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1405_2472_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> c;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> t;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">bool</span><span style="color: #000000;"> isAdded </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> TreeNode farther </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> TreeNode();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stack.Pop();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">创建节点</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> ((c </span><span style="color: #000000;">=</span><span style="color: #000000;"> stack.Pop()) </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">{</span><span style="color: #800000;">'</span><span style="color: #000000;">)
<img id="Highlighter_1631_2132_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1631_2132_Open_Text').style.display='none'; document.getElementById('Highlighter_1631_2132_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1631_2132_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1631_2132_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1631_2132_Closed_Text').style.display='none'; document.getElementById('Highlighter_1631_2132_Open_Image').style.display='inline'; document.getElementById('Highlighter_1631_2132_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1631_2132_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1631_2132_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> TreeNode node </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> TreeNode();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> node.data </span><span style="color: #000000;">=</span><span style="color: #000000;"> c;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> node.lchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> node.rchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> ((c </span><span style="color: #000000;">=</span><span style="color: #000000;"> stack.Pop()) </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">,</span><span style="color: #800000;">'</span><span style="color: #000000;">)
<img id="Highlighter_1847_2018_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1847_2018_Open_Text').style.display='none'; document.getElementById('Highlighter_1847_2018_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1847_2018_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1847_2018_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1847_2018_Closed_Text').style.display='none'; document.getElementById('Highlighter_1847_2018_Open_Image').style.display='inline'; document.getElementById('Highlighter_1847_2018_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1847_2018_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1847_2018_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">isAdded)
<img id="Highlighter_1903_1973_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1903_1973_Open_Text').style.display='none'; document.getElementById('Highlighter_1903_1973_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1903_1973_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1903_1973_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1903_1973_Closed_Text').style.display='none'; document.getElementById('Highlighter_1903_1973_Open_Image').style.display='inline'; document.getElementById('Highlighter_1903_1973_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1903_1973_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_1903_1973_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> farther.lchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> node;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
<img id="Highlighter_2056_2118_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2056_2118_Open_Text').style.display='none'; document.getElementById('Highlighter_2056_2118_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2056_2118_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2056_2118_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2056_2118_Closed_Text').style.display='none'; document.getElementById('Highlighter_2056_2118_Open_Image').style.display='inline'; document.getElementById('Highlighter_2056_2118_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2056_2118_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_2056_2118_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> farther.rchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> node;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果字符串的栈不为空</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (stack.Count </span><span style="color: #000000;">&#62;</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)
<img id="Highlighter_2203_2462_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2203_2462_Open_Text').style.display='none'; document.getElementById('Highlighter_2203_2462_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2203_2462_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2203_2462_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2203_2462_Closed_Text').style.display='none'; document.getElementById('Highlighter_2203_2462_Open_Image').style.display='inline'; document.getElementById('Highlighter_2203_2462_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2203_2462_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_2203_2462_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> farther.data </span><span style="color: #000000;">=</span><span style="color: #000000;"> stack.Pop();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">将在节点栈里面的节点组合在一起</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> farther </span><span style="color: #000000;">=</span><span style="color: #000000;"> AddToNode(farther);
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">这里节点组合后节点栈就为空了，再进栈</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> stackTree.Push(farther);
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stack.Push(farther.data);
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> TreeNode AddToNode(TreeNode node)
<img id="Highlighter_2531_3490_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2531_3490_Open_Text').style.display='none'; document.getElementById('Highlighter_2531_3490_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2531_3490_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2531_3490_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2531_3490_Closed_Text').style.display='none'; document.getElementById('Highlighter_2531_3490_Open_Image').style.display='inline'; document.getElementById('Highlighter_2531_3490_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2531_3490_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_2531_3490_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> length </span><span style="color: #000000;">=</span><span style="color: #000000;"> stackTree.Count;
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (length </span><span style="color: #000000;">&#62;</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"> </span><span style="color: #000000;">&#38;&#38;</span><span style="color: #000000;"> stackTree.Count </span><span style="color: #000000;">&#62;</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)
<img id="Highlighter_2642_3454_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2642_3454_Open_Text').style.display='none'; document.getElementById('Highlighter_2642_3454_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2642_3454_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2642_3454_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2642_3454_Closed_Text').style.display='none'; document.getElementById('Highlighter_2642_3454_Open_Image').style.display='inline'; document.getElementById('Highlighter_2642_3454_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2642_3454_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_2642_3454_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> TreeNode tnode </span><span style="color: #000000;">=</span><span style="color: #000000;"> stackTree.Pop();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果栈里面的节点的值相同就组合</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (node.rchild </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;"> </span><span style="color: #000000;">&#38;&#38;</span><span style="color: #000000;"> node.rchild.data </span><span style="color: #000000;">==</span><span style="color: #000000;"> tnode.data)
<img id="Highlighter_2819_2879_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2819_2879_Open_Text').style.display='none'; document.getElementById('Highlighter_2819_2879_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2819_2879_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2819_2879_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2819_2879_Closed_Text').style.display='none'; document.getElementById('Highlighter_2819_2879_Open_Image').style.display='inline'; document.getElementById('Highlighter_2819_2879_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2819_2879_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_2819_2879_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> node.rchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> tnode;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
<img id="Highlighter_2917_3007_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2917_3007_Open_Text').style.display='none'; document.getElementById('Highlighter_2917_3007_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2917_3007_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2917_3007_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2917_3007_Closed_Text').style.display='none'; document.getElementById('Highlighter_2917_3007_Open_Image').style.display='inline'; document.getElementById('Highlighter_2917_3007_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2917_3007_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_2917_3007_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">否则再进栈</span><span style="color: #008000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> stackTree.Push(tnode);
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (stackTree.Count </span><span style="color: #000000;">&#62;</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)
<img id="Highlighter_3066_3414_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_3066_3414_Open_Text').style.display='none'; document.getElementById('Highlighter_3066_3414_Closed_Image').style.display='inline'; document.getElementById('Highlighter_3066_3414_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_3066_3414_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_3066_3414_Closed_Text').style.display='none'; document.getElementById('Highlighter_3066_3414_Open_Image').style.display='inline'; document.getElementById('Highlighter_3066_3414_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_3066_3414_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_3066_3414_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> tnode </span><span style="color: #000000;">=</span><span style="color: #000000;"> stackTree.Pop();
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (node.lchild </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;"> </span><span style="color: #000000;">&#38;&#38;</span><span style="color: #000000;"> node.lchild.data </span><span style="color: #000000;">==</span><span style="color: #000000;"> tnode.data)
<img id="Highlighter_3212_3280_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_3212_3280_Open_Text').style.display='none'; document.getElementById('Highlighter_3212_3280_Closed_Image').style.display='inline'; document.getElementById('Highlighter_3212_3280_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_3212_3280_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_3212_3280_Closed_Text').style.display='none'; document.getElementById('Highlighter_3212_3280_Open_Image').style.display='inline'; document.getElementById('Highlighter_3212_3280_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_3212_3280_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_3212_3280_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> node.lchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> tnode;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
<img id="Highlighter_3326_3396_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_3326_3396_Open_Text').style.display='none'; document.getElementById('Highlighter_3326_3396_Closed_Image').style.display='inline'; document.getElementById('Highlighter_3326_3396_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_3326_3396_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_3326_3396_Closed_Text').style.display='none'; document.getElementById('Highlighter_3326_3396_Open_Image').style.display='inline'; document.getElementById('Highlighter_3326_3396_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_3326_3396_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">...</span><span id="Highlighter_3326_3396_Open_Text"><span style="color: #000000;">{
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stackTree.Push(tnode);
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> length</span><span style="color: #000000;">--</span><span style="color: #000000;">;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> node;
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;">
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span></div>
</div>
标答的技巧很巧妙，过一段时间让朋友帖上来。]]></description>
			<content:encoded><![CDATA[<p>昨天同事说了一道题，这道题说难也难，说简单也简单，但是细节比较麻烦，是个难题，如果这个是面试题的话，确实能够拦下很多人。</p>
<p>题目：有一串字符串，根据字符串生成二叉树，输入输出如下（输出是以树的形式输出）。</p>
<p>输入：{1}</p>
<p>输出：1</p>
<p>输入：{1{2,3}}</p>
<p>输出：根节点1，1的左节点为2，1的右节点为3</p>
<p>输入：{1{2{4},3{5,6}}}</p>
<p>输出：根节点1，1的左节点为2，2的左节点为4，右节点为空，1的右节点为3，3的左右节点分别为5，6。</p>
<p>下面是代码，慎看，并不一定是最好的，与标答还是有一定的距离的，不过效率还不错，标答就比较难理解了，过一段时间我再把标答发上来。思路不是很难，但是代码估计很难看懂：（</p>
<div class="code">
<div><img id="Highlighter_0_3522_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_0_3522_Closed_Text').style.display='none'; document.getElementById('Highlighter_0_3522_Open_Image').style.display='inline'; document.getElementById('Highlighter_0_3522_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif" alt="" align="top" /><img id="Highlighter_0_3522_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_0_3522_Open_Text').style.display='none'; document.getElementById('Highlighter_0_3522_Closed_Image').style.display='inline'; document.getElementById('Highlighter_0_3522_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gif" alt="" align="top" /><span id="Highlighter_0_3522_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">生成树</span><span id="Highlighter_0_3522_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 生成树</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img id="Highlighter_13_180_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_13_180_Closed_Text').style.display='none'; document.getElementById('Highlighter_13_180_Open_Image').style.display='inline'; document.getElementById('Highlighter_13_180_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_13_180_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_13_180_Open_Text').style.display='none'; document.getElementById('Highlighter_13_180_Closed_Image').style.display='inline'; document.getElementById('Highlighter_13_180_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /></span><span id="Highlighter_13_180_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">思路</span><span id="Highlighter_13_180_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> 思路</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">我们遍历字符串并压入到堆里面，如{1{2{4},3}}<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">我们找到}字符的位置，然后此处到前面{符号的<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">字符都出栈，生成树节点，即生成了2和2的左节点<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">然后我们再进栈，直到3的位置，我们再出栈，同理<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">我们可以生成1，2，3这三个节点，即1左节点为2<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">1的右节点为3</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img id="Highlighter_182_3510_Closed_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_182_3510_Closed_Text').style.display='none'; document.getElementById('Highlighter_182_3510_Open_Image').style.display='inline'; document.getElementById('Highlighter_182_3510_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /><img id="Highlighter_182_3510_Open_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_182_3510_Open_Text').style.display='none'; document.getElementById('Highlighter_182_3510_Closed_Image').style.display='inline'; document.getElementById('Highlighter_182_3510_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /></span><span id="Highlighter_182_3510_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff;">Code</span><span id="Highlighter_182_3510_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;"> Code</span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> TreeGenerator<br />
<img id="Highlighter_220_3498_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_220_3498_Open_Text').style.display='none'; document.getElementById('Highlighter_220_3498_Closed_Image').style.display='inline'; document.getElementById('Highlighter_220_3498_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_220_3498_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_220_3498_Closed_Text').style.display='none'; document.getElementById('Highlighter_220_3498_Open_Image').style.display='inline'; document.getElementById('Highlighter_220_3498_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /></span><span id="Highlighter_220_3498_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_220_3498_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> TreeNode<br />
<img id="Highlighter_245_342_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_245_342_Open_Text').style.display='none'; document.getElementById('Highlighter_245_342_Closed_Image').style.display='inline'; document.getElementById('Highlighter_245_342_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_245_342_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_245_342_Closed_Text').style.display='none'; document.getElementById('Highlighter_245_342_Open_Image').style.display='inline'; document.getElementById('Highlighter_245_342_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_245_342_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_245_342_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> data;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> TreeNode lchild;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> TreeNode rchild;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program<br />
<img id="Highlighter_366_3496_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_366_3496_Open_Text').style.display='none'; document.getElementById('Highlighter_366_3496_Closed_Image').style.display='inline'; document.getElementById('Highlighter_366_3496_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_366_3496_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_366_3496_Closed_Text').style.display='none'; document.getElementById('Highlighter_366_3496_Open_Image').style.display='inline'; document.getElementById('Highlighter_366_3496_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_366_3496_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_366_3496_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">一个存放字符串的栈</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> Stack</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> stack;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">一个存放节点的栈</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> Stack</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">TreeNode</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> stackTree;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Main(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)<br />
<img id="Highlighter_532_713_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_532_713_Open_Text').style.display='none'; document.getElementById('Highlighter_532_713_Closed_Image').style.display='inline'; document.getElementById('Highlighter_532_713_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_532_713_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_532_713_Closed_Text').style.display='none'; document.getElementById('Highlighter_532_713_Open_Image').style.display='inline'; document.getElementById('Highlighter_532_713_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_532_713_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_532_713_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stack </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Stack</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stackTree </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Stack</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">TreeNode</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> treecode </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">&#8220;</span><span style="color: #800000;">{1{2,3{2,2}}}</span><span style="color: #800000;">&#8220;</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> GenerateNodes(treecode);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> GenerateNodes(</span><span style="color: #0000ff;">string</span><span style="color: #000000;"> treecode)<br />
<img id="Highlighter_774_1356_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_774_1356_Open_Text').style.display='none'; document.getElementById('Highlighter_774_1356_Closed_Image').style.display='inline'; document.getElementById('Highlighter_774_1356_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_774_1356_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_774_1356_Closed_Text').style.display='none'; document.getElementById('Highlighter_774_1356_Open_Image').style.display='inline'; document.getElementById('Highlighter_774_1356_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_774_1356_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_774_1356_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果为{1}则直接生成退出</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (treecode.Length </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">3</span><span style="color: #000000;">)<br />
<img id="Highlighter_854_1026_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_854_1026_Open_Text').style.display='none'; document.getElementById('Highlighter_854_1026_Closed_Image').style.display='inline'; document.getElementById('Highlighter_854_1026_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_854_1026_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_854_1026_Closed_Text').style.display='none'; document.getElementById('Highlighter_854_1026_Open_Image').style.display='inline'; document.getElementById('Highlighter_854_1026_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_854_1026_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_854_1026_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> TreeNode tpnode </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> TreeNode();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> tpnode.data </span><span style="color: #000000;">=</span><span style="color: #000000;"> treecode[</span><span style="color: #800080;">1</span><span style="color: #000000;">];<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stackTree.Push(tpnode);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">否则遍历字符串</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> treecode.Length; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br />
<img id="Highlighter_1116_1346_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1116_1346_Open_Text').style.display='none'; document.getElementById('Highlighter_1116_1346_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1116_1346_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1116_1346_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1116_1346_Closed_Text').style.display='none'; document.getElementById('Highlighter_1116_1346_Open_Image').style.display='inline'; document.getElementById('Highlighter_1116_1346_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1116_1346_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1116_1346_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stack.Push(treecode[i]);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果不是}就进栈</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (treecode[i] </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800000;">&#8216;</span><span style="color: #800000;">}</span><span style="color: #800000;">&#8216;</span><span style="color: #000000;">)<br />
<img id="Highlighter_1243_1332_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1243_1332_Open_Text').style.display='none'; document.getElementById('Highlighter_1243_1332_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1243_1332_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1243_1332_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1243_1332_Closed_Text').style.display='none'; document.getElementById('Highlighter_1243_1332_Open_Image').style.display='inline'; document.getElementById('Highlighter_1243_1332_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1243_1332_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1243_1332_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">否则就创建节点</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> PopAndCreateNode();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> PopAndCreateNode()<br />
<img id="Highlighter_1405_2472_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1405_2472_Open_Text').style.display='none'; document.getElementById('Highlighter_1405_2472_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1405_2472_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1405_2472_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1405_2472_Closed_Text').style.display='none'; document.getElementById('Highlighter_1405_2472_Open_Image').style.display='inline'; document.getElementById('Highlighter_1405_2472_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1405_2472_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1405_2472_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> c;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> t;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">bool</span><span style="color: #000000;"> isAdded </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> TreeNode farther </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> TreeNode();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stack.Pop();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">创建节点</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> ((c </span><span style="color: #000000;">=</span><span style="color: #000000;"> stack.Pop()) </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800000;">&#8216;</span><span style="color: #800000;">{</span><span style="color: #800000;">&#8216;</span><span style="color: #000000;">)<br />
<img id="Highlighter_1631_2132_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1631_2132_Open_Text').style.display='none'; document.getElementById('Highlighter_1631_2132_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1631_2132_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1631_2132_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1631_2132_Closed_Text').style.display='none'; document.getElementById('Highlighter_1631_2132_Open_Image').style.display='inline'; document.getElementById('Highlighter_1631_2132_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1631_2132_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1631_2132_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> TreeNode node </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> TreeNode();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> node.data </span><span style="color: #000000;">=</span><span style="color: #000000;"> c;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> node.lchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> node.rchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> ((c </span><span style="color: #000000;">=</span><span style="color: #000000;"> stack.Pop()) </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800000;">&#8216;</span><span style="color: #800000;">,</span><span style="color: #800000;">&#8216;</span><span style="color: #000000;">)<br />
<img id="Highlighter_1847_2018_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1847_2018_Open_Text').style.display='none'; document.getElementById('Highlighter_1847_2018_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1847_2018_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1847_2018_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1847_2018_Closed_Text').style.display='none'; document.getElementById('Highlighter_1847_2018_Open_Image').style.display='inline'; document.getElementById('Highlighter_1847_2018_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1847_2018_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1847_2018_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">isAdded)<br />
<img id="Highlighter_1903_1973_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_1903_1973_Open_Text').style.display='none'; document.getElementById('Highlighter_1903_1973_Closed_Image').style.display='inline'; document.getElementById('Highlighter_1903_1973_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_1903_1973_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_1903_1973_Closed_Text').style.display='none'; document.getElementById('Highlighter_1903_1973_Open_Image').style.display='inline'; document.getElementById('Highlighter_1903_1973_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_1903_1973_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_1903_1973_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> farther.lchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> node;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br />
<img id="Highlighter_2056_2118_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2056_2118_Open_Text').style.display='none'; document.getElementById('Highlighter_2056_2118_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2056_2118_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2056_2118_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2056_2118_Closed_Text').style.display='none'; document.getElementById('Highlighter_2056_2118_Open_Image').style.display='inline'; document.getElementById('Highlighter_2056_2118_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2056_2118_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2056_2118_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> farther.rchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> node;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果字符串的栈不为空</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (stack.Count </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)<br />
<img id="Highlighter_2203_2462_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2203_2462_Open_Text').style.display='none'; document.getElementById('Highlighter_2203_2462_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2203_2462_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2203_2462_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2203_2462_Closed_Text').style.display='none'; document.getElementById('Highlighter_2203_2462_Open_Image').style.display='inline'; document.getElementById('Highlighter_2203_2462_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2203_2462_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2203_2462_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> farther.data </span><span style="color: #000000;">=</span><span style="color: #000000;"> stack.Pop();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">将在节点栈里面的节点组合在一起</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> farther </span><span style="color: #000000;">=</span><span style="color: #000000;"> AddToNode(farther);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">这里节点组合后节点栈就为空了，再进栈</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> stackTree.Push(farther);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stack.Push(farther.data);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> TreeNode AddToNode(TreeNode node)<br />
<img id="Highlighter_2531_3490_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2531_3490_Open_Text').style.display='none'; document.getElementById('Highlighter_2531_3490_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2531_3490_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2531_3490_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2531_3490_Closed_Text').style.display='none'; document.getElementById('Highlighter_2531_3490_Open_Image').style.display='inline'; document.getElementById('Highlighter_2531_3490_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2531_3490_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2531_3490_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> length </span><span style="color: #000000;">=</span><span style="color: #000000;"> stackTree.Count;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (length </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"> </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> stackTree.Count </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)<br />
<img id="Highlighter_2642_3454_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2642_3454_Open_Text').style.display='none'; document.getElementById('Highlighter_2642_3454_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2642_3454_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2642_3454_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2642_3454_Closed_Text').style.display='none'; document.getElementById('Highlighter_2642_3454_Open_Image').style.display='inline'; document.getElementById('Highlighter_2642_3454_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2642_3454_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2642_3454_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> TreeNode tnode </span><span style="color: #000000;">=</span><span style="color: #000000;"> stackTree.Pop();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">如果栈里面的节点的值相同就组合</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (node.rchild </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;"> </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> node.rchild.data </span><span style="color: #000000;">==</span><span style="color: #000000;"> tnode.data)<br />
<img id="Highlighter_2819_2879_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2819_2879_Open_Text').style.display='none'; document.getElementById('Highlighter_2819_2879_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2819_2879_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2819_2879_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2819_2879_Closed_Text').style.display='none'; document.getElementById('Highlighter_2819_2879_Open_Image').style.display='inline'; document.getElementById('Highlighter_2819_2879_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2819_2879_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2819_2879_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> node.rchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> tnode;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br />
<img id="Highlighter_2917_3007_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_2917_3007_Open_Text').style.display='none'; document.getElementById('Highlighter_2917_3007_Closed_Image').style.display='inline'; document.getElementById('Highlighter_2917_3007_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_2917_3007_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_2917_3007_Closed_Text').style.display='none'; document.getElementById('Highlighter_2917_3007_Open_Image').style.display='inline'; document.getElementById('Highlighter_2917_3007_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_2917_3007_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_2917_3007_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #008000;">//</span><span style="color: #008000;">否则再进栈</span><span style="color: #008000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /></span><span style="color: #000000;"> stackTree.Push(tnode);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (stackTree.Count </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)<br />
<img id="Highlighter_3066_3414_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_3066_3414_Open_Text').style.display='none'; document.getElementById('Highlighter_3066_3414_Closed_Image').style.display='inline'; document.getElementById('Highlighter_3066_3414_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_3066_3414_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_3066_3414_Closed_Text').style.display='none'; document.getElementById('Highlighter_3066_3414_Open_Image').style.display='inline'; document.getElementById('Highlighter_3066_3414_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_3066_3414_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_3066_3414_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> tnode </span><span style="color: #000000;">=</span><span style="color: #000000;"> stackTree.Pop();<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (node.lchild </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;"> </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> node.lchild.data </span><span style="color: #000000;">==</span><span style="color: #000000;"> tnode.data)<br />
<img id="Highlighter_3212_3280_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_3212_3280_Open_Text').style.display='none'; document.getElementById('Highlighter_3212_3280_Closed_Image').style.display='inline'; document.getElementById('Highlighter_3212_3280_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_3212_3280_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_3212_3280_Closed_Text').style.display='none'; document.getElementById('Highlighter_3212_3280_Open_Image').style.display='inline'; document.getElementById('Highlighter_3212_3280_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_3212_3280_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_3212_3280_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> node.lchild </span><span style="color: #000000;">=</span><span style="color: #000000;"> tnode;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br />
<img id="Highlighter_3326_3396_Open_Image" onclick="this.style.display='none'; document.getElementById('Highlighter_3326_3396_Open_Text').style.display='none'; document.getElementById('Highlighter_3326_3396_Closed_Image').style.display='inline'; document.getElementById('Highlighter_3326_3396_Closed_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockStart.gif" alt="" align="top" /><img id="Highlighter_3326_3396_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Highlighter_3326_3396_Closed_Text').style.display='none'; document.getElementById('Highlighter_3326_3396_Open_Image').style.display='inline'; document.getElementById('Highlighter_3326_3396_Open_Text').style.display='inline';" src="/fckeditor/editor/plugins/insertcode/images/ContractedSubBlock.gif" alt="" align="top" /> </span><span id="Highlighter_3326_3396_Closed_Text" style="border: 1px solid #808080; background-color: #ffffff; display: none;">&#8230;</span><span id="Highlighter_3326_3396_Open_Text"><span style="color: #000000;">{<br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> stackTree.Push(tnode);<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> length</span><span style="color: #000000;">&#8211;</span><span style="color: #000000;">;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> node;<br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /> }</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" />}</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedSubBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br />
<img src="/fckeditor/editor/plugins/insertcode/images/InBlock.gif" alt="" align="top" /><br />
<img src="/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif" alt="" align="top" /></span><span style="color: #0000ff;">#endregion</span></span></div>
</div>
<p>标答的技巧很巧妙，过一段时间让朋友帖上来。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jguoer.com/blog/index.php/archives/221/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>神奇的xor异或运算</title>
		<link>http://www.jguoer.com/blog/index.php/archives/222</link>
		<comments>http://www.jguoer.com/blog/index.php/archives/222#comments</comments>
		<pubDate>Mon, 23 Nov 2009 05:58:00 +0000</pubDate>
		<dc:creator>诡异的西红柿</dc:creator>
				<category><![CDATA[趣题]]></category>

		<guid isPermaLink="false">/post/2009/11/23/Amazing-XOR-in-PC.aspx</guid>
		<description><![CDATA[今天在逛鲁攀的QQ空间，看到一个很有意思的题目，也从另一方面告诉我们了编程中数学的重要性，很多时候数学能够给好的算法和高效的速度提供灵感，例如一个非常复杂的问题，我们却可以用几步就可以实现。

在看到这个题目之后，我就在网上查了一下这个题目并看到了异或运算的神奇，因为在说到xor异或运算的时候，大部分人都不会认为xor异或运算有什么有意思的事情，但是xor异或运算真的很神奇，不如我们首先先看个游戏，这个游戏的名字叫<a href="http://baike.baidu.com/view/1101962.html" target="_blank">Nim游戏</a>。而我们要看到的就是Nim游戏的必胜法则。（即在何种情况下无论对手如何走自己都会必胜）

<strong>有若干堆石子，每堆石子的数量都是有限的，合法的移动是“选择一堆石子并拿走若干颗（不能不拿）”，如果轮到某个人时所有的石子堆都已经被拿空了，则判负（因为他此刻没有任何合法的移动）。</strong>

这个游戏貌似是很老很老的游戏，但是直到20世纪初才被一位数学家所发现规律，所以难度可见一斑，Nim游戏是博弈论中最经典的模型之一，所以现在我们当下的人去写Nim游戏会发现其实很简单，但是我们都是站在巨人的肩膀上去看问题的。从百科里我们可以知道如下内容：

Nim游戏是组合游戏(Combinatorial Games)的一种，准确来说，属于“Impartial Combinatorial Games”（以下简称ICG）
