<?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>Kada的学习生活 &#187; 名题</title>
	<atom:link href="http://blog.okkey.net/tag/%e5%90%8d%e9%a2%98/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.okkey.net</link>
	<description>程序设计、互联网、趣味生活</description>
	<lastBuildDate>Tue, 07 Sep 2010 02:39:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>反转的数字相加</title>
		<link>http://blog.okkey.net/1102.html</link>
		<comments>http://blog.okkey.net/1102.html#comments</comments>
		<pubDate>Wed, 24 Feb 2010 14:38:13 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=1102</guid>
		<description><![CDATA[　　数字的反转数：一个数，将其首尾倒置。例如4321的反转数便是1234。 　　编写一个程序，接受两个数字，并输出他们的反转数的和的反转数。例如，24和1。那么结果就是42+1->43->34 　　注意，数字反转以后，数字的最前面和最后面不可能出现0！那么在先处理两个数相加的结果时，应该将最后方的0删除掉！ 　　如何避免多次的数字反转操作呢？可以参考自定义大数相加的算法！将数字按位相加，进行进位操作等！ 　　但是这里应该把两个数高位对齐再相加,相加时,从最高位加到最低位,并向低位进位，因为最后要反转过来！例如：1234与891的反转和，4321+198->4519->9154。表示为1234+8910->1+8, 2+9, 3+1+1, 4+0->9154。只需要一次的反转操作！ 　　注意，这里，接受两个数字的时候，按照字符串接收！ #include &#60;iostream&#62; #include &#60;vector&#62; #include &#60;string&#62; using namespace std; int main&#40;&#41; &#123; &#160; &#160; string sa,sb,st; &#160; &#160; vector&#60;int&#62; v; &#160; &#160; vector&#60;int&#62;::iterator it; &#160; &#160; int i,j,a,b; &#160; &#160; int &#8230; <a href="http://blog.okkey.net/1102.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　数字的反转数：一个数，将其首尾倒置。例如4321的反转数便是1234。</p>
<p>　　编写一个程序，接受两个数字，并输出他们的反转数的和的反转数。例如，24和1。那么结果就是42+1->43->34</p>
<p>　　注意，数字反转以后，数字的最前面和最后面不可能出现0！那么在先处理两个数相加的结果时，应该将最后方的0删除掉！</p>
<p>　　如何避免多次的数字反转操作呢？可以参考自定义大数相加的算法！将数字按位相加，进行进位操作等！</p>
<p>　　但是这里应该把两个数高位对齐再相加,相加时,从最高位加到最低位,并向低位进位，因为最后要反转过来！例如：1234与891的反转和，4321+198->4519->9154。表示为1234+8910->1+8, 2+9, 3+1+1, 4+0->9154。只需要一次的反转操作！<span id="more-1102"></span></p>
<p>　　注意，这里，接受两个数字的时候，按照字符串接收！</p>
<div class="codecolorer-container cpp dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;iostream&gt;</span><br />
<span style="color: #339900;">#include &lt;vector&gt;</span><br />
<span style="color: #339900;">#include &lt;string&gt;</span><br />
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; string sa,sb,st<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> v<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> it<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> i,j,a,b<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> sum<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> p<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> &nbsp; &nbsp;<span style="color: #666666;">// 进位标志</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> u<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> &nbsp; &nbsp;<span style="color: #666666;">// 输出标志</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000dd;">cin</span><span style="color: #000080;">&gt;&gt;</span>sa<span style="color: #000080;">&gt;&gt;</span>sb<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// 保证sa长度大于sb</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>sa.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;</span>sb .<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; st<span style="color: #000080;">=</span>sa<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sa<span style="color: #000080;">=</span>sb<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sb<span style="color: #000080;">=</span>st<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>sa.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; a <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>sa<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #FF0000;">'0'</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>i <span style="color: #000080;">&gt;=</span> sb.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>sb<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #FF0000;">'0'</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; sum <span style="color: #000080;">=</span> a<span style="color: #000040;">+</span>b<span style="color: #000040;">+</span>p<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; p <span style="color: #000080;">=</span> sum <span style="color: #000040;">/</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; v.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>sum<span style="color: #000040;">%</span><span style="color:#800080;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// 最后一次进位</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>p<span style="color: #000080;">==</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; v.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// 删除后面的所有0</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 向量的最后一个元素的it位置为end()-1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; it <span style="color: #000080;">=</span> v.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #000080;">==</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// 实现从左边第一个非零开始输出</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>j<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>j<span style="color: #000080;">&lt;</span>v.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>u<span style="color: #000080;">==</span><span style="color: #0000dd;">0</span> <span style="color: #000040;">&amp;&amp;</span> v<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #000040;">!</span><span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; u<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>u<span style="color: #000080;">==</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span>v<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/1102.html/feed</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
		<item>
		<title>求数字根小算法</title>
		<link>http://blog.okkey.net/1097.html</link>
		<comments>http://blog.okkey.net/1097.html#comments</comments>
		<pubDate>Tue, 23 Feb 2010 04:25:21 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=1097</guid>
		<description><![CDATA[　　一个正整数的数字根是通过计算该整数的各位的和产生的。如果一个整数的个位和是一位整数,那么这个数字就是该整数的数字根。如果该整数的各位和是两位或多位整数,那么,就需要重复计算各位的和,直到获得一位整数。 　　例如,考虑正整数 24。把 2 与 4 相加得到 6。由于 6 是一个一位整数,所以,6 就是24 的数字根。现在再来考虑正整数 39。3 与 9 相加等于 12。因为 12 不是一位整数,因而,需要重复处理。再把 1 加 2 得到 3,现在 3 已是一个一位整数了,那么 3 就是 39 的数字根。 　　编写一个程序，接受一个正整数，并求出他的数字根。 　　非常简单的题目。将接收到的正整数当作字符串，没一位都对应成0到9中的一位，加起来。如果加起来的和不是各位数，则重复这一过程。那么可以清除的得到递归或者循环的思路！ #include &#60;iostream&#62; #include &#60;sstream&#62; #include &#60;string&#62; #include &#60;map&#62; &#8230; <a href="http://blog.okkey.net/1097.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　一个正整数的数字根是通过计算该整数的各位的和产生的。如果一个整数的个位和是一位整数,那么这个数字就是该整数的数字根。如果该整数的各位和是两位或多位整数,那么,就需要重复计算各位的和,直到获得一位整数。</p>
<p>　　例如,考虑正整数 24。把 2 与 4 相加得到 6。由于 6 是一个一位整数,所以,6 就是24 的数字根。现在再来考虑正整数 39。3 与 9 相加等于 12。因为 12 不是一位整数,因而,需要重复处理。再把 1 加 2 得到 3,现在 3 已是一个一位整数了,那么 3 就是 39 的数字根。<span id="more-1097"></span></p>
<p>　　编写一个程序，接受一个正整数，并求出他的数字根。</p>
<p>　　非常简单的题目。将接收到的正整数当作字符串，没一位都对应成0到9中的一位，加起来。如果加起来的和不是各位数，则重复这一过程。那么可以清除的得到递归或者循环的思路！</p>
<div class="codecolorer-container cpp dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;iostream&gt;</span><br />
<span style="color: #339900;">#include &lt;sstream&gt;</span><br />
<span style="color: #339900;">#include &lt;string&gt;</span><br />
<span style="color: #339900;">#include &lt;map&gt;</span><br />
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// 字符到数字的映射 '0'-&gt;0,'1'-&gt;1...</span><br />
map<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">char</span>,<span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> m<span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// 初始化映射</span><br />
<span style="color: #0000ff;">void</span> init<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">'0'</span><span style="color: #000040;">+</span>i<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>i<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #666666;">// 将数字转换为字符串</span><br />
string convertToString<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span>x<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; ostringstream o<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>o<span style="color: #000080;">&lt;&lt;</span>x<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> o.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<br />
<span style="color: #666666;">// 求数字根</span><br />
<span style="color: #0000ff;">int</span> root<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string <span style="color: #000040;">&amp;</span>s<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> i,sum<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; string ss<span style="color: #000080;">=</span>s<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>ss.<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>sum<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>,i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span>ss .<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> m<span style="color: #008000;">&#91;</span>ss<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ss <span style="color: #000080;">=</span> convertToString<span style="color: #008000;">&#40;</span>sum<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> sum<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; init<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 初始化映射容器</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> n<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000dd;">cin</span><span style="color: #000080;">&gt;&gt;</span>n<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span>root<span style="color: #008000;">&#40;</span>convertToString<span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>　　如果仔细观察一下数字根的产生规则，可以发现更方便的方法！每次取出数字中的一位与剩下的相加，循环往复到之剩下一位数为止。比如12345，那么它的数字根可以这样产生，1234+5得到1239，123+9得到132，13+2得到15，1+5得到６。与一次性分解开来相加是一样的结果。而实现这样的加法只需用%运算即可。</p>
<div class="codecolorer-container cpp dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;iostream&gt;</span><br />
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> sum<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cin</span><span style="color: #000080;">&gt;&gt;</span>sum<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>sum<span style="color: #000080;">&gt;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sum<span style="color: #000080;">=</span>sum<span style="color: #000040;">/</span><span style="color: #0000dd;">10</span><span style="color: #000040;">+</span>sum<span style="color: #000040;">%</span><span style="color:#800080;">10</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;</span> <span style="color: #000080;">&lt;</span>sum<span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>　　两个程序真实天壤之别阿！哈哈！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/1097.html/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>C语言名题系列：连续整数和</title>
		<link>http://blog.okkey.net/1081.html</link>
		<comments>http://blog.okkey.net/1081.html#comments</comments>
		<pubDate>Mon, 08 Feb 2010 06:41:37 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=1081</guid>
		<description><![CDATA[　　编写一个程序，读入一个正整数，把所有的连续的，和与给定的正整数相等的正整数们找出来。例如，如果输入为27，于是发现2+3+4+5+6+7，8+9+10,13+14为27，那么共有3个组合。如果输入的数字是10000，那么就有18加到142，297加到1328，388加到412以及1998加到2002这4组。 　　通常的算法是，将所有小于n/2的连续正整数的和的情况算出来与n比较。 #include &#60;stdio.h&#62; #include &#60;stdlib.h&#62; void main&#40;&#41; &#123; &#160; &#160; char line&#91;100&#93;; &#160; &#160; long sum, i, j, n; &#160; &#160; int count = 0; &#160; &#160; printf&#40;&#34;请输入整数n:&#34;&#41;; &#160; &#160; gets&#40;line&#41;; &#160; &#160; n = atol&#40;line&#41;; &#160; &#160; for&#40;i=1; &#8230; <a href="http://blog.okkey.net/1081.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　编写一个程序，读入一个正整数，把所有的连续的，和与给定的正整数相等的正整数们找出来。例如，如果输入为27，于是发现2+3+4+5+6+7，8+9+10,13+14为27，那么共有3个组合。如果输入的数字是10000，那么就有18加到142，297加到1328，388加到412以及1998加到2002这4组。</p>
<p>　　通常的算法是，将所有小于n/2的连续正整数的和的情况算出来与n比较。<span id="more-1081"></span></p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span><br />
<br />
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> line<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">long</span> sum<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> n<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;请输入整数n:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gets<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; n <span style="color: #339933;">=</span> atol<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span>n<span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>sum<span style="color: #339933;">=</span>i<span style="color: #339933;">,</span>j<span style="color: #339933;">=</span>i<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> j<span style="color: #339933;">&lt;=</span>n<span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum <span style="color: #339933;">+=</span> j<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>sum <span style="color: #339933;">==</span> n<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d -- %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>count <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;共有%d种连续整数和等于%ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> count<span style="color: #339933;">,</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;没有解<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　但是这样的算法太慢速了！进行了太多的加法！因为在程序中，i与j的关系永远满足1< =i<=n,i+1<=j<=n/2+1,i<j。每一组i与j都会做一次加法，所以总共做了近似n^2/8个加法.如果n稍微大，这是非常耗时的！</p>
<p>　　如何减少加法的次数呢？可以参考<a href="http://blog.okkey.net/850.html" target="_blank">等值首尾和</a>的算法思路。</p>
<p>　　先求出一个和，sum是1+2加到i,并且sum是第一个大于等于给定值n的整数和。例如，如果n为27,那么sum=1+2+3+4+5+6+7=28。于是，如果i+(i+1)加到j大于n 那么去掉小的一端，及sum变为(i+1)+(i+2)加到j。相应的如果小于sum小于n，则大的一端在增加,即sum=i+(i+1)加到j+(j+1)。不断重复，直到i大于n/2。</p>
<p>　　代码如下：</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span><br />
<br />
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">long</span> left<span style="color: #339933;">,</span> right<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">long</span> sum<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">long</span> n<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> line<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;请输入n:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gets<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; n <span style="color: #339933;">=</span> atol<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>sum<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>right<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> sum<span style="color: #339933;">&lt;</span>n <span style="color: #339933;">;</span> sum<span style="color: #339933;">+=</span>right<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>left<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> right<span style="color: #339933;">--;</span> left<span style="color: #339933;">&lt;=</span>n<span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>sum <span style="color: #339933;">&gt;</span> n<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum <span style="color: #339933;">-=</span> <span style="color: #009900;">&#40;</span>left<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>sum <span style="color: #339933;">==</span> n<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d -- %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> left<span style="color: #339933;">,</span> right<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>right<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>count <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;共有%d种连续整数和等于%ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> count<span style="color: #339933;">,</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;没有解<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/1081.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>C语言名题系列：简单的fibonacci数列算法</title>
		<link>http://blog.okkey.net/973.html</link>
		<comments>http://blog.okkey.net/973.html#comments</comments>
		<pubDate>Sun, 31 Jan 2010 10:38:54 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=973</guid>
		<description><![CDATA[　　Fibonacci数列是指1， 1， 2， 3， 5， 8， 13， 21， 34， ····这样的数列。用式子描述即f1, f2, fn, f1=1, f2=1, 其后fn=fn-1+fn-2。 　　现在考虑，如何写一个程序，接受参数n，返回第n个fibonacci数值呢？ 　　通常的办法是用递归：即从n开始，依次调用自身求出f(n-1)和f(n-2)。代码如下： unsigned long fibonacci&#40;int n&#41; &#123; &#160; &#160; if&#40;n==2 &#124;&#124; n==1&#41; &#160; &#160; &#160; &#160; return 1; &#160; &#160; else &#160; &#160; &#160; &#160; &#8230; <a href="http://blog.okkey.net/973.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　Fibonacci数列是指1， 1， 2， 3， 5， 8， 13， 21， 34， ····这样的数列。用式子描述即f1, f2, fn, f1=1, f2=1, 其后fn=fn-1+fn-2。</p>
<p>　　现在考虑，如何写一个程序，接受参数n，返回第n个fibonacci数值呢？</p>
<p>　　通常的办法是用递归：即从n开始，依次调用自身求出f(n-1)和f(n-2)。代码如下：<span id="more-973"></span></p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> fibonacci<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">==</span><span style="color: #0000dd;">2</span> <span style="color: #339933;">||</span> n<span style="color: #339933;">==</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> fibonacci<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> fibonacci<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　虽然一目了然，但是效率实在太低。比如计算fibonacci(8)时，递归调用计算f(7)与f(6)，而计算f(7)时，又计算f(6)与f(5）,那么f(6)的计算就重复了！如此一出来，计算f(n)时，将会重复计算n-3次！n不需要很大时，计算出f(n)都已经是相当慢的！</p>
<p>　　根据fibonacci的性质，可以设计一种非递归的方法。效率会高一些。取代递归的，当然是循环。</p>
<p>　　思路：因为计算f(n）时，需要计算f(n-1)+f(n-2)。如果程序保留了f(n),f(n-1),f(n-2)。那么计算f(n+1)时。需要计算f(n）+ f(n-1)。那么要保留f(n-1)与f(n)丢掉f(n-2)。程序中，用f1与f2代表fn-2与fn-1，temp是fn-2，当计算出f2后，调整f2与f1的值，进行新的f1与f2的计算。<br />
　　代码如下：</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">unsinged <span style="color: #993333;">long</span> fibonacci<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> temp<span style="color: #339933;">,</span> f1<span style="color: #339933;">,</span> f2<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>f1<span style="color: #339933;">=</span>f2<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;=</span>n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; temp <span style="color: #339933;">=</span> f1<span style="color: #339933;">+</span>f2<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; f1<span style="color: #339933;">=</span>f2<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; f2<span style="color: #339933;">=</span>temp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> f2<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　其实求f(n)时还有一个可行的办法，可以马上得到值，而不用花费太多时间。那就是先将所求范围内的f[n]数列的结果算出来。存到数组里面。直接通过下标访问即可。</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#define MAX 1000</span><br />
<br />
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> fib<span style="color: #009900;">&#91;</span>MAX<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">void</span> genfib<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; fib<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> fib<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>MAX<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fib<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> fib<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>fib<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
usigned <span style="color: #993333;">long</span> fibonacci<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> fib<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　这个算法很简单，也很高效，但是要预先耗费太多的内存！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/973.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>C语言名题系列：快速的指数运算算法</title>
		<link>http://blog.okkey.net/939.html</link>
		<comments>http://blog.okkey.net/939.html#comments</comments>
		<pubDate>Fri, 29 Jan 2010 14:21:50 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=939</guid>
		<description><![CDATA[　　如果n与m是整数，那么m^n就是求m的n此方，即把m连乘n次！ 　　用算法描述起来很简单！在n次循环内不断自乘m，或者递归调用n次乘法即可，但是这极其没有效率！ 　　其实，可以将指数运算分解。注意到x^4可以由x^2自乘得到。 　　思路：当求m的n次方时，n可能是2k(偶数),可能是2k+1（奇数）,可能是0(不需要乘）.那么在计算m^n时，可以分成3部分进行递归。第一部分：n为0，不用计算。第2部分：n是偶数,递归计算m的n/2次方。第三部分：n是奇数，即表示为n=2k+1，那么递归调用计算m的n/2次方，再自乘m即可。 　　代码如下： unsigned long &#160;recursive_power&#40;unsigned long m, unsigned long n&#41; &#123; &#160; &#160; &#160;unsigned long temp; &#160; &#160; &#160;if &#40;n == 0&#41; &#160; &#160; &#160; &#160; &#160; &#160; &#160;// m^0 = 1 &#160; &#160; &#160; &#160; &#160; &#8230; <a href="http://blog.okkey.net/939.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　如果n与m是整数，那么m^n就是求m的n此方，即把m连乘n次！<br />
　　用算法描述起来很简单！在n次循环内不断自乘m，或者递归调用n次乘法即可，但是这极其没有效率！</p>
<p>　　其实，可以将指数运算分解。注意到x^4可以由x^2自乘得到。</p>
<p>　　思路：当求m的n次方时，n可能是2k(偶数),可能是2k+1（奇数）,可能是0(不需要乘）.那么在计算m^n时，可以分成3部分进行递归。第一部分：n为0，不用计算。第2部分：n是偶数,递归计算m的n/2次方。第三部分：n是奇数，即表示为n=2k+1，那么递归调用计算m的n/2次方，再自乘m即可。<span id="more-939"></span><br />
　　代码如下：</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;recursive_power<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> m<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> temp<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// m^0 = 1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&amp;</span> 0x01UL <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#123;</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// n为偶数则n % 2 == 0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 注意：n &amp; 0x01UL == 0判断是否偶数更有效率</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;temp <span style="color: #339933;">=</span> recursive_power<span style="color: #009900;">&#40;</span>m<span style="color: #339933;">,</span> n <span style="color: #339933;">&gt;&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> temp <span style="color: #339933;">*</span> temp<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// m^(2k) = (m^k)^2</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">else</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// n为奇数, m^n=m*m^(n-1)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> m <span style="color: #339933;">*</span> recursive_power<span style="color: #009900;">&#40;</span>m<span style="color: #339933;">,</span> n<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　其实上面的算法还是效率太低了！Kada上学期的《密码学》课程中，用于产生大指数的平方重复运算，可以将计算次数减少到2Log2N！</p>
<p>　　数学描述如下：求m的n次方时，将n写成二进制：n=n0+n1^2+n2^2+···+nk-1^k-1,其中ni为0或1，i为0到k-1。比如45用二进制表示为101101，那么m^45=m^(2^5)m^(2^3)m^(2^1)m^(2^0);</p>
<p>　　思路如下：在二进制表示中，从右往左的第i位，那么指数中就有m^(2^i)这一项，从右往左，如果第i位为1，可以把m^(2^i)乘到一个临时保存变量的结果当中（初值为1）。</p>
<p>　　如何计算m^(2^i)呢？要先将10进制的n不断mod2运算，得到二进制表示？其实两个过程可以同时进行！<br />
　　代码如下：</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> iterative_power<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> m<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;temp <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&amp;</span> 0x01UL <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// n mod 2 = 1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp <span style="color: #339933;">*=</span> m<span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// m^(2^i)在结果中</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m <span style="color: #339933;">*=</span> m<span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 平方重复</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n <span style="color: #339933;">&gt;&gt;=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// n = n/2 进行下一轮二进制转换</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> temp<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/939.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>C语言名题系列：因子分解</title>
		<link>http://blog.okkey.net/935.html</link>
		<comments>http://blog.okkey.net/935.html#comments</comments>
		<pubDate>Thu, 28 Jan 2010 13:11:25 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=935</guid>
		<description><![CDATA[　　根据算数基本定理，任何一个整数n&#62;1都可以表示为素数的乘积，切在不考虑顺序的情况下，该表达式是唯一的！即n=p1·p2·····pn。 　　写一个程序，读入一个正整数，把它的所有质因子找出来。例如，输入是72，72=2³·3²。质因数有2和3，为方便起见，将输出写成因数(指数)的形式！如输入72，则输出为2(3)3(2)。 　　思路：用质数依次去除n，从2开始一直到除不尽为止。如果除了i次，那么有2(i)。同样的道理3，5，7，···也按同样的办法处理！每一次除不尽时的余数，作为下次的被除数！ 　　代码如下： #include &#160;&#60;stdio .h&#62; #include &#160;&#60;stdlib .h&#62; #define &#160; MAXSIZE &#160;100 #define &#160; PRINT(fact, exp) printf(&#34;%ld(%ld)&#34;, fact, exp) void main&#40;void&#41; &#123; &#160; &#160; unsigned long &#160;n, work; &#160; &#160; int &#160; &#160; &#160; &#160; &#160; &#160;count = &#8230; <a href="http://blog.okkey.net/935.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　根据算数基本定理，任何一个整数n&gt;1都可以表示为素数的乘积，切在不考虑顺序的情况下，该表达式是唯一的！即n=p1·p2·····pn。</p>
<p>　　写一个程序，读入一个正整数，把它的所有质因子找出来。例如，输入是72，72=2³·3²。质因数有2和3，为方便起见，将输出写成因数(指数)的形式！如输入72，则输出为2(3)3(2)。</p>
<p>　　思路：用质数依次去除n，从2开始一直到除不尽为止。如果除了i次，那么有2(i)。同样的道理3，5，7，···也按同样的办法处理！每一次除不尽时的余数，作为下次的被除数！</p>
<p>　　代码如下：<span id="more-935"></span></p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &nbsp;&lt;stdio .h&gt;</span><br />
<span style="color: #339933;">#include &nbsp;&lt;stdlib .h&gt;</span><br />
<br />
<span style="color: #339933;">#define &nbsp; MAXSIZE &nbsp;100</span><br />
<span style="color: #339933;">#define &nbsp; PRINT(fact, exp) printf(&quot;%ld(%ld)&quot;, fact, exp)</span><br />
<br />
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;n<span style="color: #339933;">,</span> work<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i<span style="color: #339933;">,</span> k<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #339933;">*</span>dummy<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;因数分解，输入一个整数 --&gt; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gets<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; n <span style="color: #339933;">=</span> strtoul<span style="color: #009900;">&#40;</span>line<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>dummy<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 单独处理质因子2</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>work<span style="color: #339933;">=</span>n<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>work <span style="color: #339933;">&amp;</span> 0x01UL<span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> work<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> work<span style="color: #339933;">&gt;&gt;=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PRINT<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 处理其它素因子</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span> k <span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span> work<span style="color: #339933;">;</span> k <span style="color: #339933;">+=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> work <span style="color: #339933;">%</span> k <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> work <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> work <span style="color: #339933;">/=</span> k<span style="color: #339933;">,</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PRINT<span style="color: #009900;">&#40;</span>k<span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p></stdlib></stdio></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/935.html/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>C语言名题系列：线性筛法求素数</title>
		<link>http://blog.okkey.net/911.html</link>
		<comments>http://blog.okkey.net/911.html#comments</comments>
		<pubDate>Tue, 26 Jan 2010 13:47:28 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=911</guid>
		<description><![CDATA[　　在用“筛法”求素数时，每次筛除素数的动作，其实有很多是重复的！比如，如果一个数是3*5*7*9，那么删除的时候，它会被作为3的倍数，5的倍数，7的倍数，9的倍数，被重复判断删除4次。类似的，较大的数有较多的因数，那么，删除它所耗费的时间会很多！如何来优化“筛法”的算法，使之不重复呢？ 　　思路如下：从2开始，先删除2²，2³，2³，···，接着删除2·3，2²·3，2³·3，···，而此时并不删除3。再删除2·5，2²·5，2³·5，···，接下来2·7，2²·7，2³·7，···。一般而言，当发现p是一个素数时，先删除p²，p³，p³，···这一系列合数。因为p是素数，所以p²，p³，p³，···不可能被其它数整除。接着，找出比p大，但是第一个没有被删除的数q。再删除p·q，p²·q，p³·q，···。因为p是素数，q为目前为止未删除。所以p^¡·q之前也没有被删除。 　　证明上面这点很重要。如果p^i·q之前已经被删除的话，那么依照上面讲的法则。一定有一个小于p的素数a，使得存在a^k·b=p^i·q，这样，删除a的倍数时，会把p^i·q删除掉。不过，a^k·b=p^i·q并且a&#60;p这是不可能的！因为a与p都为素数，它们之间无法互相整除。为满足等式，必须有p^i整除q，即q=a^k·(b/q^i)。这样以来，q便是a的倍数了！在删除a的倍数时，q已经被删除！与q是大于p的未被删除的性质矛盾！ 　　根据上面的证明，线性筛法所做的操作不会重复！因为求小于n的素数时，p最大不会超过√n，所以p^2< =n。同样，对于任意p^i·q也必须限定在n的范围以内。 #include &#160;&#60;stdio.h&#62; #include &#160;&#60;stdlib .h&#62; #define &#160; MAXSIZE &#160; &#160;1000 #define &#160; NEXT(x) &#160; &#160;x = next[x] #define &#160; REMOVE(x) &#160;{ next[previous[x]] = next[x]; &#160; &#160; &#160; &#160; &#160;\ &#160; &#160; &#160; &#160; &#160; &#160; &#8230; <a href="http://blog.okkey.net/911.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　在用“筛法”求素数时，每次筛除素数的动作，其实有很多是重复的！比如，如果一个数是3*5*7*9，那么删除的时候，它会被作为3的倍数，5的倍数，7的倍数，9的倍数，被重复判断删除4次。类似的，较大的数有较多的因数，那么，删除它所耗费的时间会很多！如何来优化“筛法”的算法，使之不重复呢？</p>
<p>　　思路如下：从2开始，先删除2²，2³，2³，···，接着删除2·3，2²·3，2³·3，···，而此时并不删除3。再删除2·5，2²·5，2³·5，···，接下来2·7，2²·7，2³·7，···。一般而言，当发现p是一个素数时，先删除p²，p³，p³，···这一系列合数。因为p是素数，所以p²，p³，p³，···不可能被其它数整除。接着，找出比p大，但是第一个没有被删除的数q。再删除p·q，p²·q，p³·q，···。因为p是素数，q为目前为止未删除。所以p^¡·q之前也没有被删除。<span id="more-911"></span></p>
<p>　　证明上面这点很重要。如果p^i·q之前已经被删除的话，那么依照上面讲的法则。一定有一个小于p的素数a，使得存在a^k·b=p^i·q，这样，删除a的倍数时，会把p^i·q删除掉。不过，a^k·b=p^i·q并且a&lt;p这是不可能的！因为a与p都为素数，它们之间无法互相整除。为满足等式，必须有p^i整除q，即q=a^k·(b/q^i)。这样以来，q便是a的倍数了！在删除a的倍数时，q已经被删除！与q是大于p的未被删除的性质矛盾！</p>
<p>　　根据上面的证明，线性筛法所做的操作不会重复！因为求小于n的素数时，p最大不会超过√n，所以p^2< =n。同样，对于任意p^i·q也必须限定在n的范围以内。</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &nbsp;&lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &nbsp;&lt;stdlib .h&gt;</span><br />
<br />
<span style="color: #339933;">#define &nbsp; MAXSIZE &nbsp; &nbsp;1000</span><br />
<span style="color: #339933;">#define &nbsp; NEXT(x) &nbsp; &nbsp;x = next[x]</span><br />
<span style="color: #339933;">#define &nbsp; REMOVE(x) &nbsp;{ next[previous[x]] = next[x]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;previous[next[x]] = previous[x]; &nbsp; &nbsp; &nbsp;\<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span><br />
<span style="color: #339933;">#define &nbsp; INITIAL(n) { unsigned long i; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (i = 2; i &lt; = n; i++) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; previous[i] = i-1, next[i] = i+1;\<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;previous[2] = next[n] = NULL; &nbsp; &nbsp; &nbsp; &nbsp; \<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span><br />
<br />
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;previous<span style="color: #009900;">&#91;</span>MAXSIZE<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 指向前数</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;next<span style="color: #009900;">&#91;</span>MAXSIZE<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 指向后数</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;prime<span style="color: #339933;">,</span> fact<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> mult<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;n<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">char</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> dummy<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>线性筛法求素数&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>====================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>求出2到n之间的素数，输入n --&gt; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;gets<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;n <span style="color: #339933;">=</span> strtoul<span style="color: #009900;">&#40;</span>line<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>dummy<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp;INITIAL<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// 初始化序列</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>prime <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> prime<span style="color: #339933;">*</span>prime <span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span> n<span style="color: #339933;">;</span> NEXT<span style="color: #009900;">&#40;</span>prime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>fact <span style="color: #339933;">=</span> prime<span style="color: #339933;">;</span> prime<span style="color: #339933;">*</span>fact <span style="color: #339933;">&lt;=</span> n<span style="color: #339933;">;</span> NEXT<span style="color: #009900;">&#40;</span>fact<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>mult <span style="color: #339933;">=</span> prime<span style="color: #339933;">*</span>fact<span style="color: #339933;">;</span> mult <span style="color: #339933;">&lt;=</span> n<span style="color: #339933;">;</span> mult <span style="color: #339933;">*=</span> prime<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; REMOVE<span style="color: #009900;">&#40;</span>mult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 删除p^i·q</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> i <span style="color: #339933;">!=</span> NULL<span style="color: #339933;">;</span> NEXT<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>count <span style="color: #339933;">%</span> <span style="color: #0000dd;">8</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%6ld&quot;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>2到n之间的素数共有%d个&quot;</span><span style="color: #339933;">,</span> count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　如果不仔细琢磨这个程序，确实很难看懂！程序使用了3个循环来实现删除的操作。并将初始化，删除，移位的操作定义为简短的宏定义。如果实在看不懂的话，可以拿出笔来，简单的走一遍程序。弄清楚，删除以及移动目标是如何实现的！细细体会previous和next数组的作用，以及删除的实现！</p>
<p>　　比如，previous[3],previous[4],previous[5]分别为2，3，4。next[2], next[3], next[4], next[5]的值分别为3，4，5，6。再每一轮删除之后，每个previous[i]和next[i]数组中的元素将会指出i之前与之后的最近的未被删除的素数！REMOVE(4)之后，previous[5]=previous[4]=3，next[3] = next[4] = 5。换句话说，就是4被删除了，5的前面一位此时指向3，3的后面一位指向5。</p>
<p>　　真的是非常巧妙啊！Kada的笔记中贴出的代码，有些都是20年前的计算机学家或者数学家，以及专业的人士发表在相关杂志的！若非几十年的锤炼，实在难以精尖！</stdlib></stdlib></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/911.html/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>C语言名题系列：筛法求素数</title>
		<link>http://blog.okkey.net/907.html</link>
		<comments>http://blog.okkey.net/907.html#comments</comments>
		<pubDate>Tue, 26 Jan 2010 03:23:53 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=907</guid>
		<description><![CDATA[　　如果整数 n ≠ 0，±1。如果除了显然因数 ±1 和 ±n 以外，n没有其他因数，那么 n 叫做素数。如：2，3，5，7是素数，4，6，10，15是合数。 　　根据素数的性质，有这样的定理：1、设 n 是一个正合数，p 是 n 的一个大于1的最小正因数，则 p 一定是素数。2、设 n 是一个正整数，如果对所有的素数 p ≤ √n，都有 p 不整除 n，则 n 一定是素数。 　　根据两条定理，可以得到寻找素数的群定性方法，通常叫做 埃拉托斯散筛法。 　　筛法具体描述如下：对于任意给定的整数 N ，要求出所有不超过 N 的素数。列出 N 个整数，从中删除小于等于√n的所有素数p1, &#8230;, pk的倍数。然后依次删除余下的整数（不包括1）就是所要求的不超过 N &#8230; <a href="http://blog.okkey.net/907.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　如果整数 n ≠ 0，±1。如果除了显然因数 ±1 和 ±n 以外，n没有其他因数，那么 n 叫做素数。如：2，3，5，7是素数，4，6，10，15是合数。</p>
<p>　　根据素数的性质，有这样的定理：1、设 n 是一个正合数，p 是 n 的一个大于1的最小正因数，则 p 一定是素数。2、设 n 是一个正整数，如果对所有的素数 p ≤ √n，都有 p 不整除 n，则 n 一定是素数。</p>
<p>　　根据两条定理，可以得到寻找素数的群定性方法，通常叫做 埃拉托斯散筛法。<br />
　　筛法具体描述如下：对于任意给定的整数 N ，要求出所有不超过 N 的素数。列出 N 个整数，从中删除小于等于√n的所有素数p1, &#8230;, pk的倍数。然后依次删除余下的整数（不包括1）就是所要求的不超过 N 的素数。<br />
　　如果让你用筛法求出 2 到 某个数 之间的所有素数，如何用代码实现呢？</p>
<p>　　思路：用一个数组x[0], x[1], &#8230;来存储3,5,7,11&#8230;這些奇数，因此，x[i]中所有存储的数就是2i+3。将所有元素初始状态设置为未筛除，然后依次去掉其中的合数。</p>
<p>　　现在考虑如何删除合数。从x[0],x[1]&#8230;如果x[i]未被筛出，则其为质数，删除其所有倍数，依次往下走。但x[i]中储存的值是2i+3,而2i+3的倍数在数组中的什么地方呢？因为数组中全部是奇数，所以2i+3的倍数可以表示为(2n+1)(2i+3),根据<font color="red">(2n+1)(2i+3) = 2n(2i+3)+2i+3 = 2[n(2i+3)+i]+3</font>，知道x[i]对应的数(即2i+3)的倍数的位置为n(2i+3)+i。当n=1是，值为(2i+3)+i,当n=2时，值为(2i+3)+i+(2i+3), 依次类推，用(2i+3)+i作为初值，删掉对应的x[]，再加上2i+3，删掉对应的数，直到无数可删。因为最后的一个素数可以表示为2N+3,所以筛法求出的为2到N之间的素数。<br />
　　代码如下：<span id="more-907"></span></p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &nbsp;&lt;stdio .h&gt;</span><br />
<br />
<span style="color: #339933;">#define &nbsp; MAXSIZE &nbsp; 200</span><br />
<span style="color: #339933;">#define &nbsp; DELETED &nbsp; &nbsp; 1</span><br />
<span style="color: #339933;">#define &nbsp; KEPT &nbsp; &nbsp; &nbsp; &nbsp;0</span><br />
<br />
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;sieve<span style="color: #009900;">&#91;</span>MAXSIZE<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #808080; font-style: italic;">/* 待筛数组 &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;count <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">/* 素数总数 &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;prime<span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;i<span style="color: #339933;">,</span> k<span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>埃拉托斯散筛法&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>=========================&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>2 到 %d 之间的素数为：<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> MAXSIZE<span style="color: #339933;">*</span><span style="color: #0000dd;">2</span><span style="color: #339933;">+</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span> MAXSIZE<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sieve<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> KEPT<span style="color: #339933;">;</span> &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">/* 初始状态未筛出 &nbsp;*/</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> MAXSIZE<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>sieve<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> KEPT<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prime <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">=</span> prime <span style="color: #339933;">+</span> i<span style="color: #339933;">;</span> k <span style="color: #339933;">&lt;=</span> MAXSIZE<span style="color: #339933;">;</span> k <span style="color: #339933;">+=</span> prime<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sieve<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> DELETED<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//筛出</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>%6d&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> k <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> MAXSIZE<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>sieve<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> KEPT<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%6d&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>i<span style="color: #339933;">+</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>总共有 %d 个素数。&quot;</span><span style="color: #339933;">,</span> count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　其实算法简单，只不过化简了一个公式。利用了位置之间的关系。但是叙述起来比较麻烦。因为偶数不会是素数，所以本算法中，一开始存储的待筛数组全部为奇数。其实还可以在最初状态下，不考虑3的倍数，5的倍数等等。这个筛法可以再改进的！</stdio></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/907.html/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>C语言名题系列：等值首尾和</title>
		<link>http://blog.okkey.net/850.html</link>
		<comments>http://blog.okkey.net/850.html#comments</comments>
		<pubDate>Tue, 19 Jan 2010 14:28:24 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=850</guid>
		<description><![CDATA[　　假设有一个数组x[]，它有n个元素，每一个都大于零，称x[0]+x[1]+&#8230;+x[i]为前置和(Prefix_Sum)，而x[j]+x[j-1]+&#8230;+x[n-1]为后置和(Suffix_Sum)。写一个程序，求出数组x[]中有多少组相同的前置和与后置和！ 　　如果x[]的元素是3,6,2,1,4,5,2，于是x[]的前置和有以下7个，即3,9,11,12,16,21,23，而后置和有2,7,11,12,14,20,23;于是11,12与23这3对就是值相同的前置和与后置和，因为：11=3+6+2=2+5+4, 12=3+6+1=2+5+4+1。而23是整个数组的和，所以前置和与后置和相同。 　　 　　如果算出全部的和，将耗费较多的时间与空间！但是请使用更简单的算法！ 　　思路：用一个指针从头往尾走，记录下到此为止的前置和。用另一个指针，记录下到此为止的后置和！如果相等则等到一组。如果当前前置和大于后置和，则向前移动后置指针以增大后置和！相应的，用对称的方法处理后置和大于前置和的情况。 　　代码及真相如下： int &#160;head_tail&#40;int &#160;x&#91;&#93;, int n&#41; &#123; &#160; &#160; &#160;int &#160;prefix &#160; &#160; = 0, suffix &#160; &#160; = 0; &#160; &#160; &#160;int &#160;prefix_idx = 0, suffix_idx = n-1; &#160; &#160; &#160;int &#160;count = &#8230; <a href="http://blog.okkey.net/850.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　假设有一个数组x[]，它有n个元素，每一个都大于零，称x[0]+x[1]+&#8230;+x[i]为前置和(Prefix_Sum)，而x[j]+x[j-1]+&#8230;+x[n-1]为后置和(Suffix_Sum)。写一个程序，求出数组x[]中有多少组相同的前置和与后置和！</p>
<p>　　如果x[]的元素是3,6,2,1,4,5,2，于是x[]的前置和有以下7个，即3,9,11,12,16,21,23，而后置和有2,7,11,12,14,20,23;于是11,12与23这3对就是值相同的前置和与后置和，因为：11=3+6+2=2+5+4, 12=3+6+1=2+5+4+1。而23是整个数组的和，所以前置和与后置和相同。<br />
　　<span id="more-850"></span><br />
　　如果算出全部的和，将耗费较多的时间与空间！但是请使用更简单的算法！</p>
<p>　　思路：用一个指针从头往尾走，记录下到此为止的前置和。用另一个指针，记录下到此为止的后置和！如果相等则等到一组。如果当前前置和大于后置和，则向前移动后置指针以增大后置和！相应的，用对称的方法处理后置和大于前置和的情况。</p>
<p>　　代码及真相如下：</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> &nbsp;head_tail<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> &nbsp;x<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;prefix &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> suffix &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;prefix_idx <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> suffix_idx <span style="color: #339933;">=</span> n<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>suffix_idx <span style="color: #339933;">&gt;=</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> prefix_idx <span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span> n<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>prefix <span style="color: #339933;">&lt;</span> suffix<span style="color: #009900;">&#41;</span> &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">/* 前置和小 &nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prefix <span style="color: #339933;">+=</span> x<span style="color: #009900;">&#91;</span>prefix_idx<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>prefix <span style="color: #339933;">&gt;</span> suffix<span style="color: #009900;">&#41;</span> <span style="color: #808080; font-style: italic;">/* 后置和小 &nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;suffix <span style="color: #339933;">+=</span> x<span style="color: #009900;">&#91;</span>suffix_idx<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">/* 得到等值对: */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count<span style="color: #339933;">++;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* 增加计数 */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prefix <span style="color: #339933;">+=</span> x<span style="color: #009900;">&#91;</span>prefix_idx<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* 移动前后和指针*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;suffix <span style="color: #339933;">+=</span> x<span style="color: #009900;">&#91;</span>suffix_idx<span style="color: #339933;">--</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> count<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/850.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>C语言名题系列：最短距离</title>
		<link>http://blog.okkey.net/836.html</link>
		<comments>http://blog.okkey.net/836.html#comments</comments>
		<pubDate>Sat, 16 Jan 2010 12:38:48 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=836</guid>
		<description><![CDATA[　　题目：已知两个元素从小到大排列的数组x[]与y[]，请编写一个程序算出两个数组元素间彼此之间的绝对值中最小的一个数，此值叫做数组的最短距离！ 　　如果x[i]与y[j]是两个元素，那么&#124;x[i]-y[j]&#124;就是这两个元素之间的距离，所有的距离的最小值即为所求。如果x中有m个元素，y中有n个元素，则有m*n个距离！ 　　思路：利用两个数组排序好的特性，可用不必计算m*n个距离！如果d为x[i]与y[j]的距离！如果x[i]>y[j]，则y数组中y[j]前面的元素与x[i]的距离都大于d，因为数组是从小到大排列的。就像1, 4, 8, 9和0, 2, 3, 4, 10两个数组中，&#124;8-3&#124; < &#124;8-2&#124; < &#124;8-0&#124;一样！相应的：如果y[j]>x[i]，则x中x[i]前面的元素与y[j]的距离都要大于d！因此，发现x[i]>y[j]时，固定i，增加j，可以使d越来越小！直到y[j]>x[i]时，同样的道理固定j，增加i可使d越来越小！用一个变量记录下最小的d即可！ 　　代码与真相： #include &#60;limits .h&#62; #define &#160;min(x, y) &#160; &#160; ((x) &#60; (y) ? (x) : (y)) int &#160;min_distance&#40;int x&#91;&#93;, int y&#91;&#93;, int m, int n&#41; &#123; &#8230; <a href="http://blog.okkey.net/836.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　题目：已知两个元素从小到大排列的数组x[]与y[]，请编写一个程序算出两个数组元素间彼此之间的绝对值中最小的一个数，此值叫做数组的最短距离！<span id="more-836"></span></p>
<p>　　如果x[i]与y[j]是两个元素，那么|x[i]-y[j]|就是这两个元素之间的距离，所有的距离的最小值即为所求。如果x中有m个元素，y中有n个元素，则有m*n个距离！</p>
<p>　　思路：利用两个数组排序好的特性，可用不必计算m*n个距离！如果d为x[i]与y[j]的距离！如果x[i]>y[j]，则y数组中y[j]前面的元素与x[i]的距离都大于d，因为数组是从小到大排列的。就像1, 4, <font color="red">8</font>, 9和<font color="red">0, 2, 3</font>, 4, 10两个数组中，|8-3| < |8-2| < |8-0|一样！相应的：如果y[j]>x[i]，则x中x[i]前面的元素与y[j]的距离都要大于d！因此，发现x[i]>y[j]时，固定i，增加j，可以使d越来越小！直到y[j]>x[i]时，同样的道理固定j，增加i可使d越来越小！用一个变量记录下最小的d即可！<br />
　　代码与真相：</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;limits .h&gt;</span><br />
<span style="color: #339933;">#define &nbsp;min(x, y) &nbsp; &nbsp; ((x) &lt; (y) ? (x) : (y))</span><br />
<br />
<span style="color: #993333;">int</span> &nbsp;min_distance<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> m<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;minimum <span style="color: #339933;">=</span> INT_MAX<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;</span> m <span style="color: #339933;">&amp;&amp;</span> j <span style="color: #339933;">&lt;</span> n<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;=</span> y<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;minimum <span style="color: #339933;">=</span> min<span style="color: #009900;">&#40;</span>minimum<span style="color: #339933;">,</span> x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span>y<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;minimum <span style="color: #339933;">=</span> min<span style="color: #009900;">&#40;</span>minimum<span style="color: #339933;">,</span> y<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span>x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> minimum<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　说明：保存最小值的变量需要有一个初始值。其值要大于所有可能的距离值才行！所以引入了极限值INT_MAX，并且通过x[i]与y[j]的大小关系控制，避免了调用abs函数。</limits>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/836.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>C语言名题系列:等值数目</title>
		<link>http://blog.okkey.net/829.html</link>
		<comments>http://blog.okkey.net/829.html#comments</comments>
		<pubDate>Fri, 15 Jan 2010 14:10:24 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=829</guid>
		<description><![CDATA[　　题目：给定两个由递增排序好的数组，找出他们有多少等值对。如f[]:1,2,3,5,7,8和g[]:2,3,7,8有四对相等的值，所以答案为4。 　　分析：要写出好的算法还是要充分利用数组已排序好这一特性！因为两个数组都已经排序好！如果f[i]g[j]，则将g[j+1]与f[i]比较！否则它们相等，总数累加后，两个数组都向后比较。这样一来，当其中一个数组的元素被访问完时，则得到答案！其实比较的过程是两个数组一上一下，轮流向前推进的过程！ 　　 　　代码及真相在此： int coincidence_count&#40;int f&#91;&#93;, int g&#91;&#93;, int m, int n&#41; &#123; &#160; &#160; &#160;int &#160;i,j; &#160; &#160; &#160;int &#160;count; &#160; &#160; &#160;count = i = j = 0; &#160; &#160; &#160;while &#40;i &#60; m &#38;&#38; j &#60; &#8230; <a href="http://blog.okkey.net/829.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　题目：给定两个由递增排序好的数组，找出他们有多少等值对。如f[]:1,2,3,5,7,8和g[]:2,3,7,8有四对相等的值，所以答案为4。 </p>
<p>　　分析：要写出好的算法还是要充分利用数组已排序好这一特性！因为两个数组都已经排序好！如果f[i]<g [j]，则将f[i+1]与g[j]比较！如果f[i]>g[j]，则将g[j+1]与f[i]比较！否则它们相等，总数累加后，两个数组都向后比较。这样一来，当其中一个数组的元素被访问完时，则得到答案！其实比较的过程是两个数组一上一下，轮流向前推进的过程！<br />
　　<span id="more-829"></span></p>
<p>　　代码及真相在此：</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> coincidence_count<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> f<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> g<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> m<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;i<span style="color: #339933;">,</span>j<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;count<span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp;count <span style="color: #339933;">=</span> i <span style="color: #339933;">=</span> j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;</span> m <span style="color: #339933;">&amp;&amp;</span> j <span style="color: #339933;">&lt;</span> n<span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> g<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> g<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count<span style="color: #339933;">++,</span> i<span style="color: #339933;">++,</span> j<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> count<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p></g></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/829.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>C语言名题系列:支配值数目</title>
		<link>http://blog.okkey.net/811.html</link>
		<comments>http://blog.okkey.net/811.html#comments</comments>
		<pubDate>Thu, 14 Jan 2010 12:41:20 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=811</guid>
		<description><![CDATA[　　题目：给定两个排序好的数组f[]和g[]，计算满足f[i]>g[j]的对数！如f：2，3，5，7，9和g：1，2，4，6，7中，2>1,3>1,3>2,5>1,5>2,5>4,7>1,7>2,7>4,7>6,9>7,9>6,9>4,9>2,9>1！有15组这样的关系，所以答案为15。 　　老规矩，尽量使代码简短！最少的元素访问次数！ 　　本题的关键在于，利用数组排序好的性质！思路如下：f[i]大于g[j]，则f[i]后面的元素都大于g[j]。利用一个变量count来记录满足大于关系的对数，如果f[i]>g[j]时，count增加f中i后的元素个数，并将j后移，否则将i后移，拿f中更大的元素与当前g[j]比较！这样一来，当访问完其中一个数组的元素时，便得出了答案！没有必要将两个数组都走一遍！ 　　 　　代码及真相： int &#160;gt_count&#40;int f&#91;&#93;, int g&#91;&#93;, int m, int n&#41; &#123; &#160; &#160; &#160;int &#160;i, j; &#160; &#160; &#160;int &#160;count; &#160; &#160; &#160;count = i = j = 0; &#160; &#160; &#160;while &#40;i &#60; m &#38;&#38; &#8230; <a href="http://blog.okkey.net/811.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　题目：给定两个排序好的数组f[]和g[]，计算满足f[i]>g[j]的对数！如f：2，3，5，7，9和g：1，2，4，6，7中，2>1,3>1,3>2,5>1,5>2,5>4,7>1,7>2,7>4,7>6,9>7,9>6,9>4,9>2,9>1！有15组这样的关系，所以答案为15。</p>
<p>　　老规矩，尽量使代码简短！最少的元素访问次数！</p>
<p>　　本题的关键在于，利用数组排序好的性质！思路如下：f[i]大于g[j]，则f[i]后面的元素都大于g[j]。利用一个变量count来记录满足大于关系的对数，如果f[i]>g[j]时，count增加f中i后的元素个数，并将j后移，否则将i后移，拿f中更大的元素与当前g[j]比较！这样一来，当访问完其中一个数组的元素时，便得出了答案！没有必要将两个数组都走一遍！<br />
　　<span id="more-811"></span><br />
　　代码及真相：</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> &nbsp;gt_count<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> f<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> g<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> m<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;i<span style="color: #339933;">,</span> j<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;count<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;count <span style="color: #339933;">=</span> i <span style="color: #339933;">=</span> j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;</span> m <span style="color: #339933;">&amp;&amp;</span> j <span style="color: #339933;">&lt;</span> n<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> g<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j<span style="color: #339933;">++,</span> count <span style="color: #339933;">+=</span> m <span style="color: #339933;">-</span> i<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> count<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/811.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>C语言名题系列:最长平台</title>
		<link>http://blog.okkey.net/800.html</link>
		<comments>http://blog.okkey.net/800.html#comments</comments>
		<pubDate>Wed, 13 Jan 2010 14:14:18 +0000</pubDate>
		<dc:creator>Kada</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[名题]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://blog.okkey.net/?p=800</guid>
		<description><![CDATA[　　手头有一本书C语言名题集，很不错！上面大多是知名问题，也给出很多精妙的解决办法。趁着假期，接着习练。将进行连载，每天分享一个题目，敬请关注！ 　　最长平台问题：已知一个已经从小到大排序的数组，这个数组中的的一个平台（Plateau）就是连续的一串值相同的元素，并且这个串元素不能再延伸。例如，在1，2，2，3，3，3，4，5，5，6中1，2-2，3-3-3，4，5-5，6都是平台，试编写一个函数，接收一个数组，并把这个数组中的最长平台所含的个数作为返回值，如上例中3-3-3就是最长平台，该平台中一共有3个3，故函数返回值为3。 　　要求使用最少的变量，最少的查询次数以及最少的程序语句！ 　　本题目关键在于利用数组已经从小到大排序的性质！利用一个变量记录L平台长度(至少为1)，如果当前元素X[i]与其后L距离处的元素X[i+L]相等，则他们之间的元素也是相等的，因为数组时从小到大排序好的！有了这个思路，问题便简单了！ int longest_plateau&#40;int x&#91;&#93;, int n&#41; &#123; &#160; &#160; &#160;int &#160;length = 1; &#160; &#160; &#160; &#160; /* plateau length &#62;= 1. &#160; &#160; */ &#160; &#160; &#160;int &#160;i; &#160; &#160; &#160;for &#40;i = 1; i &#60; &#8230; <a href="http://blog.okkey.net/800.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>　　手头有一本书C语言名题集，很不错！上面大多是知名问题，也给出很多精妙的解决办法。趁着假期，接着习练。将进行连载，每天分享一个题目，敬请关注！<br />
　　<strong>最长平台问题</strong>：已知一个已经<strong>从小到大排序</strong>的数组，这个数组中的的一个平台（Plateau）就是连续的一串值相同的元素，并且这个串元素不能再延伸。例如，在1，2，2，3，3，3，4，5，5，6中1，2-2，3-3-3，4，5-5，6都是平台，试编写一个函数，接收一个数组，并把这个数组中的最长平台所含的个数作为返回值，如上例中3-3-3就是最长平台，该平台中一共有3个3，故函数返回值为3。<span id="more-800"></span><br />
　　<strong>要求使用最少的变量，最少的查询次数以及最少的程序语句！</strong><br />
　　本题目关键在于利用数组已经从小到大排序的性质！利用一个变量记录L平台长度(至少为1)，如果当前元素X[i]与其后L距离处的元素X[i+L]相等，则他们之间的元素也是相等的，因为数组时从小到大排序好的！有了这个思路，问题便简单了！</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> longest_plateau<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;length <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* plateau length &gt;= 1. &nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333;">int</span> &nbsp;i<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> x<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span>length<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;length<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> length<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　以上是一个计算机科学家89年给出的解法！现在来考虑问题的扩展，<strong>假若数组并未排序好，请给出找出最长平台的办法！我的思路如下：若当前比较的两个元素不相等，则将平台长度置为1，继续向后比较。返回找的的多个平台中的最大值即可！有代码有真相：</p>
<div class="codecolorer-container c dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:600px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#define max(x, y) ( (x) &gt; (y) ) ? (x) : (y)</span><br />
<span style="color: #993333;">int</span> kz_plateau<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> length <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> maxlen <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> x<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span>length<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxlen <span style="color: #339933;">=</span> max<span style="color: #009900;">&#40;</span>maxlen<span style="color: #339933;">,</span> length<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> maxlen<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.okkey.net/800.html/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
