﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Dain-随笔分类-算法</title><link>http://www.cppblog.com/dqchen/category/3246.html</link><description>写出一个可以工作的程序并不够</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 11:28:25 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 11:28:25 GMT</pubDate><ttl>60</ttl><item><title>列出所有9位数，它的前n位能被n整除</title><link>http://www.cppblog.com/dqchen/archive/2007/04/16/22030.html</link><dc:creator>Dain</dc:creator><author>Dain</author><pubDate>Mon, 16 Apr 2007 09:29:00 GMT</pubDate><guid>http://www.cppblog.com/dqchen/archive/2007/04/16/22030.html</guid><wfw:comment>http://www.cppblog.com/dqchen/comments/22030.html</wfw:comment><comments>http://www.cppblog.com/dqchen/archive/2007/04/16/22030.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/dqchen/comments/commentRss/22030.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dqchen/services/trackbacks/22030.html</trackback:ping><description><![CDATA[<p><span style="FONT-SIZE: 8pt">最简单的是穷举，不过那可要O(9*10<sup>9</sup>)，不可取&nbsp;<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>using&nbsp;namespace&nbsp;std;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;fun(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)<br><img id=Codehighlighter1_105_492_Open_Image onclick="this.style.display='none'; Codehighlighter1_105_492_Open_Text.style.display='none'; Codehighlighter1_105_492_Closed_Image.style.display='inline'; Codehighlighter1_105_492_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_105_492_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_105_492_Closed_Text.style.display='none'; Codehighlighter1_105_492_Open_Image.style.display='inline'; Codehighlighter1_105_492_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_105_492_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_105_492_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;last,all;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i,j,k;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;i&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all.push_back(i);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(n&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;all;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;size;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;num;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i&nbsp;</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">&nbsp;n;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br><img id=Codehighlighter1_258_476_Open_Image onclick="this.style.display='none'; Codehighlighter1_258_476_Open_Text.style.display='none'; Codehighlighter1_258_476_Closed_Image.style.display='inline'; Codehighlighter1_258_476_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_258_476_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_258_476_Closed_Text.style.display='none'; Codehighlighter1_258_476_Open_Image.style.display='inline'; Codehighlighter1_258_476_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_258_476_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_258_476_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;all;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all.clear();<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">)last.size();<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;j&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;size;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">j)<br><img id=Codehighlighter1_344_457_Open_Image onclick="this.style.display='none'; Codehighlighter1_344_457_Open_Text.style.display='none'; Codehighlighter1_344_457_Closed_Image.style.display='inline'; Codehighlighter1_344_457_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_344_457_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_344_457_Closed_Text.style.display='none'; Codehighlighter1_344_457_Open_Image.style.display='inline'; Codehighlighter1_344_457_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_344_457_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_344_457_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(k&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;k&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">k)<br><img id=Codehighlighter1_374_453_Open_Image onclick="this.style.display='none'; Codehighlighter1_374_453_Open_Text.style.display='none'; Codehighlighter1_374_453_Closed_Image.style.display='inline'; Codehighlighter1_374_453_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_374_453_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_374_453_Closed_Text.style.display='none'; Codehighlighter1_374_453_Open_Image.style.display='inline'; Codehighlighter1_374_453_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_374_453_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_374_453_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;last[j]&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;k;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(num&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;i&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all.push_back(num);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last.clear();<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;all;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
</span>
<img src ="http://www.cppblog.com/dqchen/aggbug/22030.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dqchen/" target="_blank">Dain</a> 2007-04-16 17:29 <a href="http://www.cppblog.com/dqchen/archive/2007/04/16/22030.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最大的子序列和问题</title><link>http://www.cppblog.com/dqchen/archive/2007/02/07/18500.html</link><dc:creator>Dain</dc:creator><author>Dain</author><pubDate>Wed, 07 Feb 2007 02:52:00 GMT</pubDate><guid>http://www.cppblog.com/dqchen/archive/2007/02/07/18500.html</guid><wfw:comment>http://www.cppblog.com/dqchen/comments/18500.html</wfw:comment><comments>http://www.cppblog.com/dqchen/archive/2007/02/07/18500.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/dqchen/comments/commentRss/18500.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dqchen/services/trackbacks/18500.html</trackback:ping><description><![CDATA[
		<font size="2">求解该问题的四种算法：<br /></font>
		<font color="#008000" size="2">
				<strong>时间<em>O(N<sup>3</sup>)，</em>算法一</strong>
				<br />
		</font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> MaxSubsequenceSum(</span>
						<span style="COLOR: #0000ff">const</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> A[],</span>
						<span style="COLOR: #0000ff">int</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000"> N)<br /><img id="Codehighlighter1_43_263_Open_Image" onclick="this.style.display='none'; Codehighlighter1_43_263_Open_Text.style.display='none'; Codehighlighter1_43_263_Closed_Image.style.display='inline'; Codehighlighter1_43_263_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_43_263_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_43_263_Closed_Text.style.display='none'; Codehighlighter1_43_263_Open_Image.style.display='inline'; Codehighlighter1_43_263_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
						<span id="Codehighlighter1_43_263_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.cppblog.com/images/dot.gif" />
						</span>
				</font>
				<span id="Codehighlighter1_43_263_Open_Text">
						<font size="2">
								<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">int</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> ThisSum,MaxSum,i,j,k;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    MaxSum </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">for</span>
								<span style="COLOR: #000000">(i </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">;i </span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #000000"> N;i</span>
								<span style="COLOR: #000000">++</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">for</span>
								<span style="COLOR: #000000">(j </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> i;j </span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #000000"> N;j</span>
								<span style="COLOR: #000000">++</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">)<br /><img id="Codehighlighter1_134_242_Open_Image" onclick="this.style.display='none'; Codehighlighter1_134_242_Open_Text.style.display='none'; Codehighlighter1_134_242_Closed_Image.style.display='inline'; Codehighlighter1_134_242_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_134_242_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_134_242_Closed_Text.style.display='none'; Codehighlighter1_134_242_Open_Image.style.display='inline'; Codehighlighter1_134_242_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        </span>
								<span id="Codehighlighter1_134_242_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
						<span id="Codehighlighter1_134_242_Open_Text">
								<font size="2">
										<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />            ThisSum </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0</span>
								</font>
								<font size="2">
										<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />            </span>
										<span style="COLOR: #0000ff">for</span>
										<span style="COLOR: #000000">(k </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> i;k </span>
										<span style="COLOR: #000000">&lt;=</span>
										<span style="COLOR: #000000"> j;k</span>
										<span style="COLOR: #000000">++</span>
										<span style="COLOR: #000000">)    ThisSum </span>
										<span style="COLOR: #000000">+=</span>
								</font>
								<font size="2">
										<span style="COLOR: #000000"> A[k];                <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />            </span>
										<span style="COLOR: #0000ff">if</span>
										<span style="COLOR: #000000">(ThisSum </span>
										<span style="COLOR: #000000">&gt;</span>
										<span style="COLOR: #000000"> MaxSum)    MaxSum </span>
										<span style="COLOR: #000000">=</span>
								</font>
								<span style="COLOR: #000000">
										<font size="2"> ThisSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</font>
								</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </font>
						</span>
						<span style="COLOR: #0000ff">
								<font size="2">return</font>
						</span>
						<span style="COLOR: #000000">
								<font size="2"> MaxSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</font>
						</span>
				</span>
		</div>
		<font color="#008000">
				<font size="2">
						<strong>时间<em>O(N<sup>2</sup>)，</em>算法二</strong>
						<br />
				</font>
				<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
						<font size="2">
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> MaxSubsequenceSum(</span>
								<span style="COLOR: #0000ff">const</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> A[],</span>
								<span style="COLOR: #0000ff">int</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> N)<br /><img id="Codehighlighter1_43_244_Open_Image" onclick="this.style.display='none'; Codehighlighter1_43_244_Open_Text.style.display='none'; Codehighlighter1_43_244_Closed_Image.style.display='inline'; Codehighlighter1_43_244_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_43_244_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_43_244_Closed_Text.style.display='none'; Codehighlighter1_43_244_Open_Image.style.display='inline'; Codehighlighter1_43_244_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
								<span id="Codehighlighter1_43_244_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
						<span id="Codehighlighter1_43_244_Open_Text">
								<font size="2">
										<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
										<span style="COLOR: #0000ff">int</span>
								</font>
								<font size="2">
										<span style="COLOR: #000000"> ThisSum,MaxSum,i,j;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    MaxSum </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0</span>
								</font>
								<font size="2">
										<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
										<span style="COLOR: #0000ff">for</span>
										<span style="COLOR: #000000">(i </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">;i </span>
										<span style="COLOR: #000000">&lt;</span>
										<span style="COLOR: #000000"> N;i</span>
										<span style="COLOR: #000000">++</span>
								</font>
								<font size="2">
										<span style="COLOR: #000000">)<br /><img id="Codehighlighter1_108_223_Open_Image" onclick="this.style.display='none'; Codehighlighter1_108_223_Open_Text.style.display='none'; Codehighlighter1_108_223_Closed_Image.style.display='inline'; Codehighlighter1_108_223_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_108_223_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_108_223_Closed_Text.style.display='none'; Codehighlighter1_108_223_Open_Image.style.display='inline'; Codehighlighter1_108_223_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
										<span id="Codehighlighter1_108_223_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
												<img src="http://www.cppblog.com/images/dot.gif" />
										</span>
								</font>
								<span id="Codehighlighter1_108_223_Open_Text">
										<font size="2">
												<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        ThisSum </span>
												<span style="COLOR: #000000">=</span>
												<span style="COLOR: #000000"> </span>
												<span style="COLOR: #000000">0</span>
										</font>
										<font size="2">
												<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
												<span style="COLOR: #0000ff">for</span>
												<span style="COLOR: #000000">(j </span>
												<span style="COLOR: #000000">=</span>
												<span style="COLOR: #000000"> i;j </span>
												<span style="COLOR: #000000">&lt;</span>
												<span style="COLOR: #000000"> N;j</span>
												<span style="COLOR: #000000">++</span>
										</font>
										<font size="2">
												<span style="COLOR: #000000">)<br /><img id="Codehighlighter1_150_220_Open_Image" onclick="this.style.display='none'; Codehighlighter1_150_220_Open_Text.style.display='none'; Codehighlighter1_150_220_Closed_Image.style.display='inline'; Codehighlighter1_150_220_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_150_220_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_150_220_Closed_Text.style.display='none'; Codehighlighter1_150_220_Open_Image.style.display='inline'; Codehighlighter1_150_220_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        </span>
												<span id="Codehighlighter1_150_220_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
														<img src="http://www.cppblog.com/images/dot.gif" />
												</span>
										</font>
										<span id="Codehighlighter1_150_220_Open_Text">
												<font size="2">
														<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />            ThisSum </span>
														<span style="COLOR: #000000">+=</span>
												</font>
												<font size="2">
														<span style="COLOR: #000000"> A[k];                <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />            </span>
														<span style="COLOR: #0000ff">if</span>
														<span style="COLOR: #000000">(ThisSum </span>
														<span style="COLOR: #000000">&gt;</span>
														<span style="COLOR: #000000"> MaxSum)    MaxSum </span>
														<span style="COLOR: #000000">=</span>
												</font>
												<span style="COLOR: #000000">
														<font size="2"> ThisSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</font>
												</span>
										</span>
										<span style="COLOR: #000000">
												<br />
												<font size="2">
														<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</font>
										</span>
								</span>
								<span style="COLOR: #000000">
										<br />
										<font size="2">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </font>
								</span>
								<span style="COLOR: #0000ff">
										<font size="2">return</font>
								</span>
								<span style="COLOR: #000000">
										<font size="2"> MaxSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</font>
								</span>
						</span>
				</div>
		</font>
		<font color="#008000" size="2">
				<strong>时间<em>O(NlogN)</em>，算法三</strong>
				<br />
		</font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #0000ff">static</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> MaxSubSum(</span>
						<span style="COLOR: #0000ff">const</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> A[],</span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> Left,</span>
						<span style="COLOR: #0000ff">int</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000"> Right)<br /><img id="Codehighlighter1_55_818_Open_Image" onclick="this.style.display='none'; Codehighlighter1_55_818_Open_Text.style.display='none'; Codehighlighter1_55_818_Closed_Image.style.display='inline'; Codehighlighter1_55_818_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_55_818_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_55_818_Closed_Text.style.display='none'; Codehighlighter1_55_818_Open_Image.style.display='inline'; Codehighlighter1_55_818_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
						<span id="Codehighlighter1_55_818_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.cppblog.com/images/dot.gif" />
						</span>
				</font>
				<span id="Codehighlighter1_55_818_Open_Text">
						<font size="2">
								<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">int</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> MaxLeftSum,MaxRightSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">int</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> MaxLeftBorderSum,MaxRightBorderSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">int</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> LeftBorderSum,RightBorderSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">int</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> Center,i;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000">(Left </span>
								<span style="COLOR: #000000">==</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> Right)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000">(A[left] </span>
								<span style="COLOR: #000000">&gt;</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">)    </span>
								<span style="COLOR: #0000ff">return</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> A[left];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #0000ff">else</span>
								<span style="COLOR: #000000">    </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />            <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    Center </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> (Left </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000"> Right) </span>
								<span style="COLOR: #000000">/</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">2</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    MaxLeftSum </span>
								<span style="COLOR: #000000">=</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> MaxSubSum(A,Left,Center);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    MaxRightSum </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> MaxSubSum(A,Center </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">1</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">,Right);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    MaxLeftBorderSum </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    LeftBorderSum </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">for</span>
								<span style="COLOR: #000000">(i </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> Center;i </span>
								<span style="COLOR: #000000">&gt;=</span>
								<span style="COLOR: #000000"> Left;i</span>
								<span style="COLOR: #000000">--</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">)<br /><img id="Codehighlighter1_446_547_Open_Image" onclick="this.style.display='none'; Codehighlighter1_446_547_Open_Text.style.display='none'; Codehighlighter1_446_547_Closed_Image.style.display='inline'; Codehighlighter1_446_547_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_446_547_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_446_547_Closed_Text.style.display='none'; Codehighlighter1_446_547_Open_Image.style.display='inline'; Codehighlighter1_446_547_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
								<span id="Codehighlighter1_446_547_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
						<span id="Codehighlighter1_446_547_Open_Text">
								<font size="2">
										<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        LeftBorderSum </span>
										<span style="COLOR: #000000">+=</span>
								</font>
								<font size="2">
										<span style="COLOR: #000000"> A[i];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
										<span style="COLOR: #0000ff">if</span>
										<span style="COLOR: #000000">(LeftBorderSum </span>
										<span style="COLOR: #000000">&gt;</span>
										<span style="COLOR: #000000"> MaxLeftBorderSum)    MaxLeftBorderSum </span>
										<span style="COLOR: #000000">=</span>
								</font>
								<span style="COLOR: #000000">
										<font size="2"> LeftBorderSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</font>
								</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    MaxRightBorderSum </font>
						</span>
						<font size="2">
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    RightBorderSum </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">for</span>
								<span style="COLOR: #000000">(i </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> Center </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">1</span>
								<span style="COLOR: #000000">;i </span>
								<span style="COLOR: #000000">&lt;=</span>
								<span style="COLOR: #000000"> Right;i</span>
								<span style="COLOR: #000000">++</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">)<br /><img id="Codehighlighter1_633_739_Open_Image" onclick="this.style.display='none'; Codehighlighter1_633_739_Open_Text.style.display='none'; Codehighlighter1_633_739_Closed_Image.style.display='inline'; Codehighlighter1_633_739_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_633_739_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_633_739_Closed_Text.style.display='none'; Codehighlighter1_633_739_Open_Image.style.display='inline'; Codehighlighter1_633_739_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
								<span id="Codehighlighter1_633_739_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
						<span id="Codehighlighter1_633_739_Open_Text">
								<font size="2">
										<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        RightBorderSum </span>
										<span style="COLOR: #000000">+=</span>
								</font>
								<font size="2">
										<span style="COLOR: #000000"> A[i];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
										<span style="COLOR: #0000ff">if</span>
										<span style="COLOR: #000000">(RightBorderSum </span>
										<span style="COLOR: #000000">&gt;</span>
										<span style="COLOR: #000000"> MaxRightBorderSum)    MaxRightBorderSum </span>
										<span style="COLOR: #000000">=</span>
								</font>
								<span style="COLOR: #000000">
										<font size="2"> RightBorderSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</font>
								</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </font>
						</span>
						<font size="2">
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> Max3(MaxLeftSum,MaxRightSum,MaxLeftBorderSum </span>
								<span style="COLOR: #000000">+</span>
						</font>
						<span style="COLOR: #000000">
								<font size="2"> MaxRightBorderSum);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</font>
						</span>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						</font>
				</span>
				<font size="2">
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> MaxSubsequenceSum(</span>
						<span style="COLOR: #0000ff">const int</span> <span style="COLOR: #000000"> A[],</span><span style="COLOR: #0000ff">int</span></font>
				<font size="2">
						<span style="COLOR: #000000"> N)<br /><img id="Codehighlighter1_860_893_Open_Image" onclick="this.style.display='none'; Codehighlighter1_860_893_Open_Text.style.display='none'; Codehighlighter1_860_893_Closed_Image.style.display='inline'; Codehighlighter1_860_893_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_860_893_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_860_893_Closed_Text.style.display='none'; Codehighlighter1_860_893_Open_Image.style.display='inline'; Codehighlighter1_860_893_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
						<span id="Codehighlighter1_860_893_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.cppblog.com/images/dot.gif" />
						</span>
				</font>
				<span id="Codehighlighter1_860_893_Open_Text">
						<font size="2">
								<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> MaxSubSum(A,</span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">,N </span>
								<span style="COLOR: #000000">-</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">1</span>
						</font>
						<span style="COLOR: #000000">
								<font size="2">);    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</font>
						</span>
				</span>
		</div>
		<font size="2">
				<font color="#008000">
						<strong>时间<em>O(N)</em>，算法四</strong>
				</font>
				<br />
				<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #000000">intMaxSubsequenceSum(</span>
						<span style="COLOR: #0000ff">const int</span>
						<span style="COLOR: #000000"> A[],</span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> N)<br /><img id="Codehighlighter1_38_220_Open_Image" onclick="this.style.display='none'; Codehighlighter1_38_220_Open_Text.style.display='none'; Codehighlighter1_38_220_Closed_Image.style.display='inline'; Codehighlighter1_38_220_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_38_220_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_38_220_Closed_Text.style.display='none'; Codehighlighter1_38_220_Open_Image.style.display='inline'; Codehighlighter1_38_220_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
						<span id="Codehighlighter1_38_220_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.cppblog.com/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_38_220_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> ThisSum,MaxSum,i;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    ThisSum </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> MaxSum </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">for</span>
								<span style="COLOR: #000000">(i </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">;i </span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #000000"> N;i</span>
								<span style="COLOR: #000000">++</span>
								<span style="COLOR: #000000">)<br /><img id="Codehighlighter1_111_200_Open_Image" onclick="this.style.display='none'; Codehighlighter1_111_200_Open_Text.style.display='none'; Codehighlighter1_111_200_Closed_Image.style.display='inline'; Codehighlighter1_111_200_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_111_200_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_111_200_Closed_Text.style.display='none'; Codehighlighter1_111_200_Open_Image.style.display='inline'; Codehighlighter1_111_200_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
								<span id="Codehighlighter1_111_200_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
								<span id="Codehighlighter1_111_200_Open_Text">
										<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        ThisSum </span>
										<span style="COLOR: #000000">+=</span>
										<span style="COLOR: #000000"> A[i];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
										<span style="COLOR: #0000ff">if</span>
										<span style="COLOR: #000000">(ThisSum </span>
										<span style="COLOR: #000000">&gt;</span>
										<span style="COLOR: #000000"> MaxSum)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />            MaxSum </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> ThisSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
										<span style="COLOR: #0000ff">else</span>
										<span style="COLOR: #000000">
												<br />
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />            ThisSum </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
								</span>
								<span style="COLOR: #000000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> MaxSum;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
						</span>
				</div>
				<p align="right">
						<br />
						<font color="#0000ff">参考《数据结构与算法分析》</font>
				</p>
		</font>
<img src ="http://www.cppblog.com/dqchen/aggbug/18500.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dqchen/" target="_blank">Dain</a> 2007-02-07 10:52 <a href="http://www.cppblog.com/dqchen/archive/2007/02/07/18500.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>全排列</title><link>http://www.cppblog.com/dqchen/archive/2006/12/25/16812.html</link><dc:creator>Dain</dc:creator><author>Dain</author><pubDate>Mon, 25 Dec 2006 02:17:00 GMT</pubDate><guid>http://www.cppblog.com/dqchen/archive/2006/12/25/16812.html</guid><wfw:comment>http://www.cppblog.com/dqchen/comments/16812.html</wfw:comment><comments>http://www.cppblog.com/dqchen/archive/2006/12/25/16812.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/dqchen/comments/commentRss/16812.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dqchen/services/trackbacks/16812.html</trackback:ping><description><![CDATA[
		<p>
				<font size="2">
						<font color="#008000">
								<strong>首先，给出算法的思路</strong>
						</font>
						<br />设R={r<sub>1</sub>,r<sub>2</sub>,...,r<sub>n</sub>}是要进行排列的n个元素，R<sub>i</sub>=R-{r<sub>i</sub>}。<br />集合X中元素的全排列记为permutation(X)，(ri)permutation(X)表示在全排列permutation(X)的每一个排列前加上前缀r<sub>i</sub>得到的排列。<br />R的全排列可归纳定义如下：<br />当n=1时，permutation(R)={r},r是集合R中唯一的元素；<br />当n&gt;1时，permutation(R)由(r<sub>1</sub>)permutation(R<sub>1</sub>)，(r<sub>2</sub>)permutation(R<sub>2</sub>)，……，(r<sub>n</sub>)permutation(R<sub>n</sub>)构成。<br /><br /><font color="#0000ff">此算法要求待排列的数据是互异的，因为该算法不能检测同种排列是否已经输出，如：<br />1, 1, 2<br />那么，全排列期望输出是：<br />1, 1, 2<br />1, 2, 1<br />2, 1, 1<br /></font></font>
				<font size="2">
						<font color="#0000ff">但是该算法的输出：<br /></font>
				</font>
				<font size="2">
						<font color="#008000">
								<font color="#0000ff">
										<span style="COLOR: #000000">
												<font color="#0000ff">1, 1, 2<br />1, 2, 1<br />2, 1, 1<br /></font>
												<font color="#ff0000">1, 1, 2<br />1, 2, 1<br />2, 1, 1</font>
												<br />
										</span>
								</font>
								<font color="#0000ff">这是该算法的缺点，也限制了它的适用范围。<br /></font>
								<br />
						</font>程序描述如下：</font>
		</p>
		<div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)">
				<span style="COLOR: rgb(0,0,0)">#include </span>
				<span style="COLOR: rgb(0,0,0)">&lt;</span>
				<span style="COLOR: rgb(0,0,0)">iostream</span>
				<span style="COLOR: rgb(0,0,0)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />#include </span>
				<span style="COLOR: rgb(0,0,0)">&lt;</span>
				<span style="COLOR: rgb(0,0,0)">algorithm</span>
				<span style="COLOR: rgb(0,0,0)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)"> <br /><br /></span>
				<span style="COLOR: rgb(0,0,255)">using</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(0,0,255)">namespace</span>
				<span style="COLOR: rgb(0,0,0)"> std; <br /><br /></span>
				<span style="COLOR: rgb(0,128,0)">//</span>
				<span style="COLOR: rgb(0,128,0)"> 递归产生R[k:n]的所有的排列，<strong><font color="#ee82ee">元素是互异的</font></strong></span>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<span style="COLOR: rgb(0,0,0)">template </span>
				<span style="COLOR: rgb(0,0,0)">&lt;</span>
				<span style="COLOR: rgb(0,0,255)">class</span>
				<span style="COLOR: rgb(0,0,0)"> Type</span>
				<span style="COLOR: rgb(0,0,0)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />
				</span>
				<span style="COLOR: rgb(0,0,255)">void</span>
				<span style="COLOR: rgb(0,0,0)"> permutation(Type </span>
				<span style="COLOR: rgb(0,0,0)">*</span>
				<span style="COLOR: rgb(0,0,0)">R,</span>
				<span style="COLOR: rgb(0,0,255)">int</span>
				<span style="COLOR: rgb(0,0,0)"> k,</span>
				<span style="COLOR: rgb(0,0,255)">int</span>
				<span style="COLOR: rgb(0,0,0)"> n)<br />{<br />    </span>
				<span style="COLOR: rgb(0,0,255)">if</span>
				<span style="COLOR: rgb(0,0,0)">(k</span>
				<span style="COLOR: rgb(0,0,0)">==</span>
				<span style="COLOR: rgb(0,0,0)">n)<br />    {<br />        </span>
				<span style="COLOR: rgb(0,0,255)">for</span>
				<span style="COLOR: rgb(0,0,0)">(</span>
				<span style="COLOR: rgb(0,0,255)">int</span>
				<span style="COLOR: rgb(0,0,0)"> i</span>
				<span style="COLOR: rgb(0,0,0)">=</span>
				<span style="COLOR: rgb(0,0,0)">0</span>
				<span style="COLOR: rgb(0,0,0)">;i</span>
				<span style="COLOR: rgb(0,0,0)">&lt;</span>
				<span style="COLOR: rgb(0,0,0)">n;</span>
				<span style="COLOR: rgb(0,0,0)">++</span>
				<span style="COLOR: rgb(0,0,0)">i)<br />            cout </span>
				<span style="COLOR: rgb(0,0,0)">&lt;&lt;</span>
				<span style="COLOR: rgb(0,0,0)"> R[i] </span>
				<span style="COLOR: rgb(0,0,0)">&lt;&lt;</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(0,0,0)">"</span>
				<span style="COLOR: rgb(0,0,0)">\t</span>
				<span style="COLOR: rgb(0,0,0)">"</span>
				<span style="COLOR: rgb(0,0,0)">;<br />        cout </span>
				<span style="COLOR: rgb(0,0,0)">&lt;&lt;</span>
				<span style="COLOR: rgb(0,0,0)"> endl;<br />    }<br />    </span>
				<span style="COLOR: rgb(0,0,255)">else</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />        </span>
				<span style="COLOR: rgb(0,0,255)">for</span>
				<span style="COLOR: rgb(0,0,0)">(</span>
				<span style="COLOR: rgb(0,0,255)">int</span>
				<span style="COLOR: rgb(0,0,0)"> i</span>
				<span style="COLOR: rgb(0,0,0)">=</span>
				<span style="COLOR: rgb(0,0,0)">k;i</span>
				<span style="COLOR: rgb(0,0,0)">&lt;</span>
				<span style="COLOR: rgb(0,0,0)">n;</span>
				<span style="COLOR: rgb(0,0,0)">++</span>
				<span style="COLOR: rgb(0,0,0)">i)<br />        {<br />            swap(R[k],R[i]);<br />            permutation(R,k</span>
				<span style="COLOR: rgb(0,0,0)">+</span>
				<span style="COLOR: rgb(0,0,0)">1</span>
				<span style="COLOR: rgb(0,0,0)">,n);<br />            swap(R[k],R[i]);<br />        }<br />}</span>
		</div>
		<p>
				<font size="2">
						<font color="#008000">
								<strong>还有一种很简单的方法，使用GP中的方法<br /><br /></strong>
						</font>
						<font color="#0000ff">该算法是STL中的范型算法，当然效果是很好的，不会出现上面的算法的情况。<br /><br /></font>程序描述如下：</font>
		</p>
		<font size="2">
				<div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)">
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> 使用泛型算法next_permutation()</span>
						<span style="COLOR: rgb(0,128,0)">
								<br />
						</span>
						<span style="COLOR: rgb(0,0,0)">#include </span>
						<span style="COLOR: rgb(0,0,0)">&lt;</span>
						<span style="COLOR: rgb(0,0,0)">iostream</span>
						<span style="COLOR: rgb(0,0,0)">&gt;</span>
						<span style="COLOR: rgb(0,0,0)">
								<br />#include </span>
						<span style="COLOR: rgb(0,0,0)">&lt;</span>
						<span style="COLOR: rgb(0,0,0)">vector</span>
						<span style="COLOR: rgb(0,0,0)">&gt;</span>
						<span style="COLOR: rgb(0,0,0)">
								<br />#include </span>
						<span style="COLOR: rgb(0,0,0)">&lt;</span>
						<span style="COLOR: rgb(0,0,0)">algorithm</span>
						<span style="COLOR: rgb(0,0,0)">&gt;</span>
						<span style="COLOR: rgb(0,0,0)"> <br /><br /></span>
						<span style="COLOR: rgb(0,0,255)">using</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">namespace</span>
						<span style="COLOR: rgb(0,0,0)"> std; <br /><br /></span>
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> 产生R[k:n]的所有的排列</span>
						<span style="COLOR: rgb(0,128,0)">
								<br />
						</span>
						<span style="COLOR: rgb(0,0,0)">template </span>
						<span style="COLOR: rgb(0,0,0)">&lt;</span>
						<span style="COLOR: rgb(0,0,255)">class</span>
						<span style="COLOR: rgb(0,0,0)"> Type</span>
						<span style="COLOR: rgb(0,0,0)">&gt;</span>
						<span style="COLOR: rgb(0,0,0)"> <br /><br /></span>
						<span style="COLOR: rgb(0,0,255)">void</span>
						<span style="COLOR: rgb(0,0,0)"> pernutation(Type </span>
						<span style="COLOR: rgb(0,0,0)">*</span>
						<span style="COLOR: rgb(0,0,0)">R,</span>
						<span style="COLOR: rgb(0,0,255)">int</span>
						<span style="COLOR: rgb(0,0,0)"> k,</span>
						<span style="COLOR: rgb(0,0,255)">int</span>
						<span style="COLOR: rgb(0,0,0)"> n)<br />{<br /> vector</span>
						<span style="COLOR: rgb(0,0,0)">&lt;</span>
						<span style="COLOR: rgb(0,0,0)">Type</span>
						<span style="COLOR: rgb(0,0,0)">&gt;</span>
						<span style="COLOR: rgb(0,0,0)"> myVec;<br /> </span>
						<span style="COLOR: rgb(0,0,255)">int</span>
						<span style="COLOR: rgb(0,0,0)"> i,size </span>
						<span style="COLOR: rgb(0,0,0)">=</span>
						<span style="COLOR: rgb(0,0,0)"> n </span>
						<span style="COLOR: rgb(0,0,0)">-</span>
						<span style="COLOR: rgb(0,0,0)"> k;<br /> </span>
						<span style="COLOR: rgb(0,0,255)">for</span>
						<span style="COLOR: rgb(0,0,0)">(i </span>
						<span style="COLOR: rgb(0,0,0)">=</span>
						<span style="COLOR: rgb(0,0,0)"> k;i </span>
						<span style="COLOR: rgb(0,0,0)">&lt;</span>
						<span style="COLOR: rgb(0,0,0)"> n;i</span>
						<span style="COLOR: rgb(0,0,0)">++</span>
						<span style="COLOR: rgb(0,0,0)">)<br />  myVec.push_back(R[i]);<br /> </span>
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> 使用next_permutation()函数必须是有序的数据</span>
						<span style="COLOR: rgb(0,128,0)">
								<br />
						</span>
						<span style="COLOR: rgb(0,0,0)"> sort(myVec.begin(),myVec.end());<br />  <br /> </span>
						<span style="COLOR: rgb(0,0,255)">do</span>
						<span style="COLOR: rgb(0,0,0)">
								<br /> {<br />  </span>
						<span style="COLOR: rgb(0,0,255)">for</span>
						<span style="COLOR: rgb(0,0,0)">(i </span>
						<span style="COLOR: rgb(0,0,0)">=</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,0)">0</span>
						<span style="COLOR: rgb(0,0,0)">;i </span>
						<span style="COLOR: rgb(0,0,0)">&lt;</span>
						<span style="COLOR: rgb(0,0,0)"> size;i</span>
						<span style="COLOR: rgb(0,0,0)">++</span>
						<span style="COLOR: rgb(0,0,0)">)<br />   cout </span>
						<span style="COLOR: rgb(0,0,0)">&lt;&lt;</span>
						<span style="COLOR: rgb(0,0,0)"> myVec[i] </span>
						<span style="COLOR: rgb(0,0,0)">&lt;&lt;</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,0)">"</span>
						<span style="COLOR: rgb(0,0,0)">\t</span>
						<span style="COLOR: rgb(0,0,0)">"</span>
						<span style="COLOR: rgb(0,0,0)">;<br />  cout </span>
						<span style="COLOR: rgb(0,0,0)">&lt;&lt;</span>
						<span style="COLOR: rgb(0,0,0)"> endl;<br /> }<br /> </span>
						<span style="COLOR: rgb(0,0,255)">while</span>
						<span style="COLOR: rgb(0,0,0)">(next_permutation(myVec.begin(),myVec.end()));<br />}</span>
				</div>
				<p>注：这里的待全排的数据是存在数组或者向量中的。</p>
		</font>
<img src ="http://www.cppblog.com/dqchen/aggbug/16812.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dqchen/" target="_blank">Dain</a> 2006-12-25 10:17 <a href="http://www.cppblog.com/dqchen/archive/2006/12/25/16812.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>算法求解(还没有人做出来，唉)</title><link>http://www.cppblog.com/dqchen/archive/2006/12/21/16679.html</link><dc:creator>Dain</dc:creator><author>Dain</author><pubDate>Thu, 21 Dec 2006 02:58:00 GMT</pubDate><guid>http://www.cppblog.com/dqchen/archive/2006/12/21/16679.html</guid><wfw:comment>http://www.cppblog.com/dqchen/comments/16679.html</wfw:comment><comments>http://www.cppblog.com/dqchen/archive/2006/12/21/16679.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/dqchen/comments/commentRss/16679.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dqchen/services/trackbacks/16679.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Given an int m and a vector <int> a containing n numbers, return the result of the following expression: &nbsp;&nbsp;<a href='http://www.cppblog.com/dqchen/archive/2006/12/21/16679.html'>阅读全文</a><img src ="http://www.cppblog.com/dqchen/aggbug/16679.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dqchen/" target="_blank">Dain</a> 2006-12-21 10:58 <a href="http://www.cppblog.com/dqchen/archive/2006/12/21/16679.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>string和int间的类型转换</title><link>http://www.cppblog.com/dqchen/archive/2006/12/10/16215.html</link><dc:creator>Dain</dc:creator><author>Dain</author><pubDate>Sun, 10 Dec 2006 05:05:00 GMT</pubDate><guid>http://www.cppblog.com/dqchen/archive/2006/12/10/16215.html</guid><wfw:comment>http://www.cppblog.com/dqchen/comments/16215.html</wfw:comment><comments>http://www.cppblog.com/dqchen/archive/2006/12/10/16215.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/dqchen/comments/commentRss/16215.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/dqchen/services/trackbacks/16215.html</trackback:ping><description><![CDATA[
		<font size="2">
				<font color="#008000">
						<strong>int 转换 string</strong>
						<br />
				</font>
				<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #0000ff">string</span>
						<span style="COLOR: #000000"> int2str(</span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> num)<br /><img id="Codehighlighter1_24_265_Open_Image" onclick="this.style.display='none'; Codehighlighter1_24_265_Open_Text.style.display='none'; Codehighlighter1_24_265_Closed_Image.style.display='inline'; Codehighlighter1_24_265_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_24_265_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_24_265_Closed_Text.style.display='none'; Codehighlighter1_24_265_Open_Image.style.display='inline'; Codehighlighter1_24_265_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
						<span id="Codehighlighter1_24_265_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
								<img src="http://www.cppblog.com/images/dot.gif" />
						</span>
						<span id="Codehighlighter1_24_265_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000">(num </span>
								<span style="COLOR: #000000">==</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
								<span style="COLOR: #0000ff">string</span>
								<span style="COLOR: #000000"> str </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">""</span>
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> num_ </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> num </span>
								<span style="COLOR: #000000">&gt;</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">?</span>
								<span style="COLOR: #000000"> num : </span>
								<span style="COLOR: #000000">-</span>
								<span style="COLOR: #000000">1</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">*</span>
								<span style="COLOR: #000000"> num;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
								<span style="COLOR: #0000ff">while</span>
								<span style="COLOR: #000000">(num_)<br /><img id="Codehighlighter1_143_208_Open_Image" onclick="this.style.display='none'; Codehighlighter1_143_208_Open_Text.style.display='none'; Codehighlighter1_143_208_Closed_Image.style.display='inline'; Codehighlighter1_143_208_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_143_208_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_143_208_Closed_Text.style.display='none'; Codehighlighter1_143_208_Open_Image.style.display='inline'; Codehighlighter1_143_208_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />   </span>
								<span id="Codehighlighter1_143_208_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
								<span id="Codehighlighter1_143_208_Open_Text">
										<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      str </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> (</span>
										<span style="COLOR: #0000ff">char</span>
										<span style="COLOR: #000000">)(num_ </span>
										<span style="COLOR: #000000">%</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">10</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">+</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">48</span>
										<span style="COLOR: #000000">) </span>
										<span style="COLOR: #000000">+</span>
										<span style="COLOR: #000000"> str;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      num_ </span>
										<span style="COLOR: #000000">/=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">10</span>
										<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    }</span>
								</span>
								<span style="COLOR: #000000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000">(num </span>
								<span style="COLOR: #000000">&lt;</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      str </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">-</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000"> str;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
								<span style="COLOR: #0000ff">return</span>
								<span style="COLOR: #000000"> str;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
						</span>
				</div>
				<font color="#008000">
						<strong>string 转换 int<br /></strong>
				</font>
				<font color="#000000">
						<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #0000ff">int</span>
								<span style="COLOR: #000000"> str2int(</span>
								<span style="COLOR: #0000ff">string</span>
								<span style="COLOR: #000000"> str)<br /><img id="Codehighlighter1_24_251_Open_Image" onclick="this.style.display='none'; Codehighlighter1_24_251_Open_Text.style.display='none'; Codehighlighter1_24_251_Closed_Image.style.display='inline'; Codehighlighter1_24_251_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_24_251_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_24_251_Closed_Text.style.display='none'; Codehighlighter1_24_251_Open_Image.style.display='inline'; Codehighlighter1_24_251_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
								<span id="Codehighlighter1_24_251_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
								<span id="Codehighlighter1_24_251_Open_Text">
										<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">int</span>
										<span style="COLOR: #000000"> i,len </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> str.size(),num </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    i </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">;   <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">if</span>
										<span style="COLOR: #000000">(str[</span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">] </span>
										<span style="COLOR: #000000">==</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">'</span>
										<span style="COLOR: #000000">-</span>
										<span style="COLOR: #000000">'</span>
										<span style="COLOR: #000000">)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      i </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">1</span>
										<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">while</span>
										<span style="COLOR: #000000">(i </span>
										<span style="COLOR: #000000">&lt;</span>
										<span style="COLOR: #000000"> len)<br /><img id="Codehighlighter1_134_194_Open_Image" onclick="this.style.display='none'; Codehighlighter1_134_194_Open_Text.style.display='none'; Codehighlighter1_134_194_Closed_Image.style.display='inline'; Codehighlighter1_134_194_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_134_194_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_134_194_Closed_Text.style.display='none'; Codehighlighter1_134_194_Open_Image.style.display='inline'; Codehighlighter1_134_194_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />   </span>
										<span id="Codehighlighter1_134_194_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
												<img src="http://www.cppblog.com/images/dot.gif" />
										</span>
										<span id="Codehighlighter1_134_194_Open_Text">
												<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      num </span>
												<span style="COLOR: #000000">=</span>
												<span style="COLOR: #000000"> num </span>
												<span style="COLOR: #000000">*</span>
												<span style="COLOR: #000000"> </span>
												<span style="COLOR: #000000">10</span>
												<span style="COLOR: #000000"> </span>
												<span style="COLOR: #000000">+</span>
												<span style="COLOR: #000000"> (</span>
												<span style="COLOR: #0000ff">int</span>
												<span style="COLOR: #000000">)(str[i] </span>
												<span style="COLOR: #000000">-</span>
												<span style="COLOR: #000000"> </span>
												<span style="COLOR: #000000">'</span>
												<span style="COLOR: #000000">0</span>
												<span style="COLOR: #000000">'</span>
												<span style="COLOR: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      i</span>
												<span style="COLOR: #000000">++</span>
												<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    }</span>
										</span>
										<span style="COLOR: #000000">
												<br />
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
												<br />
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">if</span>
										<span style="COLOR: #000000">(str[</span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">] </span>
										<span style="COLOR: #000000">==</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">'</span>
										<span style="COLOR: #000000">-</span>
										<span style="COLOR: #000000">'</span>
										<span style="COLOR: #000000">)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />      num </span>
										<span style="COLOR: #000000">*=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">-</span>
										<span style="COLOR: #000000">1</span>
										<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />   </span>
										<span style="COLOR: #0000ff">return</span>
										<span style="COLOR: #000000"> num;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</span>
						</div>
						<br />string和int间是可以自己写函数实现类型转换的，但是string和double就不太方便了，stirng转换double可以自定义函数实现，但是，double转换string就需要使用库函数了，如sprintf</font>
		</font>
<img src ="http://www.cppblog.com/dqchen/aggbug/16215.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/dqchen/" target="_blank">Dain</a> 2006-12-10 13:05 <a href="http://www.cppblog.com/dqchen/archive/2006/12/10/16215.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>