﻿<?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++博客-iSsay's Summer Time-随笔分类-Algorithm</title><link>http://www.cppblog.com/issay789/category/17615.html</link><description>like a shining star</description><language>zh-cn</language><lastBuildDate>Sun, 21 Aug 2011 15:16:59 GMT</lastBuildDate><pubDate>Sun, 21 Aug 2011 15:16:59 GMT</pubDate><ttl>60</ttl><item><title>螺旋队列问题</title><link>http://www.cppblog.com/issay789/archive/2011/08/21/154036.html</link><dc:creator>Issay Tseng</dc:creator><author>Issay Tseng</author><pubDate>Sun, 21 Aug 2011 14:14:00 GMT</pubDate><guid>http://www.cppblog.com/issay789/archive/2011/08/21/154036.html</guid><wfw:comment>http://www.cppblog.com/issay789/comments/154036.html</wfw:comment><comments>http://www.cppblog.com/issay789/archive/2011/08/21/154036.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/issay789/comments/commentRss/154036.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/issay789/services/trackbacks/154036.html</trackback:ping><description><![CDATA[<p>21 22 .... <br />20 7 8 9 10 <br />19 6 1 2 11 <br />18 5 4 3 12 <br />17 16 15 14 13 <br />看清以上数字排列的规律，设 1 点的坐标是 (0,0)，x 方向向右为正，y 方向向下为正。例如，7 的坐标为 (-1,-1)，2 的坐标为 (0,1)，3 的坐标为 (1,1)。编程实现输入任意一点坐标 (x,y)，输出所对应的数字。［Finland 某著名通信设备公司 2005 年面试题］ <br />----------------------------------------------</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><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 /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;abs(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x)<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img id="Codehighlighter1_57_77_Open_Image" onclick="this.style.display='none'; Codehighlighter1_57_77_Open_Text.style.display='none'; Codehighlighter1_57_77_Closed_Image.style.display='inline'; Codehighlighter1_57_77_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_57_77_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_57_77_Closed_Text.style.display='none'; Codehighlighter1_57_77_Open_Image.style.display='inline'; Codehighlighter1_57_77_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_57_77_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_57_77_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;x</span><span style="color: #000000">&gt;</span><span style="color: #000000">0</span><span style="color: #000000">?</span><span style="color: #000000">x:</span><span style="color: #000000">-</span><span style="color: #000000">x;<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;getVal(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;y)<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img id="Codehighlighter1_104_509_Open_Image" onclick="this.style.display='none'; Codehighlighter1_104_509_Open_Text.style.display='none'; Codehighlighter1_104_509_Closed_Image.style.display='inline'; Codehighlighter1_104_509_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_104_509_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_104_509_Closed_Text.style.display='none'; Codehighlighter1_104_509_Open_Image.style.display='inline'; Codehighlighter1_104_509_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_104_509_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_104_509_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,s;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">n是层数，s是前一圈末最后一个数&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">12</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;val;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">结果</span><span style="color: #008000"><br /></span><span style="color: #008080">13</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">0</span><span style="color: #000000">==</span><span style="color: #000000">x&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">==</span><span style="color: #000000">y)&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;n</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;abs(x)</span><span style="color: #000000">&gt;</span><span style="color: #000000">abs(y)</span><span style="color: #000000">?</span><span style="color: #000000">abs(x):abs(y);&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">确定圈数&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">16</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;s</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">2</span><span style="color: #000000">*</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)</span><span style="color: #000000">*</span><span style="color: #000000">(</span><span style="color: #000000">2</span><span style="color: #000000">*</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">计算钱一圈最后一个数值&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">18</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(x</span><span style="color: #000000">==</span><span style="color: #000000">n&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;y</span><span style="color: #000000">!=-</span><span style="color: #000000">n)&nbsp;val</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;s</span><span style="color: #000000">+</span><span style="color: #000000">n</span><span style="color: #000000">+</span><span style="color: #000000">y;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">第一列，s+n为y轴的原点，y正方向递增&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">19</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(y</span><span style="color: #000000">==</span><span style="color: #000000">n&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;x</span><span style="color: #000000">!=</span><span style="color: #000000">n)&nbsp;val</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">*</span><span style="color: #000000">n</span><span style="color: #000000">+</span><span style="color: #000000">s</span><span style="color: #000000">+</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">x;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">第二列，2*n是前一列增值&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">20</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(x</span><span style="color: #000000">==-</span><span style="color: #000000">n&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;y</span><span style="color: #000000">!=</span><span style="color: #000000">n)&nbsp;val</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">*</span><span style="color: #000000">2</span><span style="color: #000000">*</span><span style="color: #000000">n</span><span style="color: #000000">+</span><span style="color: #000000">s</span><span style="color: #000000">+</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">y;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">第三列，加上前2列的列增值&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">21</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(y</span><span style="color: #000000">==-</span><span style="color: #000000">n&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;x</span><span style="color: #000000">!=-</span><span style="color: #000000">n)&nbsp;val</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">3</span><span style="color: #000000">*</span><span style="color: #000000">2</span><span style="color: #000000">*</span><span style="color: #000000">n</span><span style="color: #000000">+</span><span style="color: #000000">s</span><span style="color: #000000">+</span><span style="color: #000000">n</span><span style="color: #000000">+</span><span style="color: #000000">x;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">第四列&nbsp;，加上前3列的增值&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">22</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;val;<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">25</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">26</span><span style="color: #000000"><img id="Codehighlighter1_523_666_Open_Image" onclick="this.style.display='none'; Codehighlighter1_523_666_Open_Text.style.display='none'; Codehighlighter1_523_666_Closed_Image.style.display='inline'; Codehighlighter1_523_666_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_523_666_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_523_666_Closed_Text.style.display='none'; Codehighlighter1_523_666_Open_Image.style.display='inline'; Codehighlighter1_523_666_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_523_666_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_523_666_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=-</span><span style="color: #000000">4</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">4</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img id="Codehighlighter1_552_653_Open_Image" onclick="this.style.display='none'; Codehighlighter1_552_653_Open_Text.style.display='none'; Codehighlighter1_552_653_Closed_Image.style.display='inline'; Codehighlighter1_552_653_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_552_653_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_552_653_Closed_Text.style.display='none'; Codehighlighter1_552_653_Open_Image.style.display='inline'; Codehighlighter1_552_653_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_552_653_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_552_653_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=-</span><span style="color: #000000">4</span><span style="color: #000000">;&nbsp;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">4</span><span style="color: #000000">;&nbsp;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img id="Codehighlighter1_583_636_Open_Image" onclick="this.style.display='none'; Codehighlighter1_583_636_Open_Text.style.display='none'; Codehighlighter1_583_636_Closed_Image.style.display='inline'; Codehighlighter1_583_636_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_583_636_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_583_636_Closed_Text.style.display='none'; Codehighlighter1_583_636_Open_Image.style.display='inline'; Codehighlighter1_583_636_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_583_636_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_583_636_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">31</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">getVal(j,i)</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">\t</span><span style="color: #000000">"</span><span style="color: #000000">;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">注意先变的是x，所以j做横坐标&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">32</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">33</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /></span><span style="color: #008080">34</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">35</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">36</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">37</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">38</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: #008080">39</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>
<p>&nbsp;</p>
<p>&nbsp;</p><img src ="http://www.cppblog.com/issay789/aggbug/154036.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/issay789/" target="_blank">Issay Tseng</a> 2011-08-21 22:14 <a href="http://www.cppblog.com/issay789/archive/2011/08/21/154036.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>