- 浏览: 908501 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
yaawaas:
有了 。。
程序员装B指南 -
niepeng880208:
藐视我还是初级装
程序员装B指南 -
sa364867195:
哥们,现在实现了吗?
GPS卫星定位车辆监控系统 -
月下小人:
顶,这才是质的提高,果断顶
一个基于jQuery ajax和.net httphandler 的超轻异步框架,千行代码完成。 -
xp9802:
有意思,动静结合
程序员装B指南
一道有趣的循环题目
<p><span style="font-size: 9pt;"><span style="">问题:输入为</span></span><span style="" lang="EN-US">n</span><span style="font-size: 9pt;"><span style="">,求一个</span></span><span style="" lang="EN-US">n*n</span><span style="font-size: 9pt;"><span style="">的矩阵,规定沿</span></span><span style="" lang="EN-US">45</span><span style="font-size: 9pt;"><span style="">度线递增,形成一个</span></span><span style="" lang="EN-US">zigzag</span><span style="font-size: 9pt;"><span style="">数组(</span></span><span style="" lang="EN-US">JEPG</span><span style="font-size: 9pt;"><span style="">编码里取像素数据的排列顺序),用</span></span><span style="" lang="EN-US">C++</span><span style=""><span style="font-size: 9pt;">实现。</span><span style="" lang="EN-US"></span></span></p>
<p><span style="" lang="EN-US"></span></p>
<p><span style=""><span style="font-size: 9pt;">本人的实现思路(在《程序员面试宝典》中有另一种解法):</span><span style="" lang="EN-US"></span></span></p>
<p><span style="font-size: 9pt;"><span style="">问题的实质是把</span></span><span style="" lang="EN-US">[0,n-1]</span><span style=""><span style="font-size: 9pt;">中的整数依之字形填入一个二维数组。于是考虑按之字形遍历数组,遍历时,方向共有三个:</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">1.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">起始时向右一位:</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">a)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果没有填充完毕则向左下方斜行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">2.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">向左下方斜行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">a)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果没有碰到边界,则继续向左下方斜行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">b)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果碰到左边界而没碰到下边界;则向下直行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">c)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果碰到下边界(不管有没有碰到左边界),则向右直行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">3.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">向右上方斜行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">a)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果没有碰到边界,则继续向右上方斜行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">b)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果碰到上边界而没有碰到右边界,向右直行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">c)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果碰到右边界(无论有没有碰到上边界),则向下直行;</span><span style="" lang="EN-US"></span></span></p>
<p><span style="font-size: 9pt;"><span style="">填充数字够</span></span><span style="" lang="EN-US">n*n</span><span style="font-size: 9pt;"><span style="">个时结束填充,</span></span><span style="" lang="EN-US">C++</span><span style=""><span style="font-size: 9pt;">代码如下:</span><span style="" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">#include</span><span style="" lang="EN-US"> <span style="color: #a31515;">"stdafx.h"</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">#include</span><span style="" lang="EN-US"> <span style="color: #a31515;"><iostream></span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">#include</span><span style="" lang="EN-US"> <span style="color: #a31515;"><iomanip></span> </span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">#include</span><span style="" lang="EN-US"> <span style="color: #a31515;"><cassert></span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">using</span><span style="" lang="EN-US"> <span style="color: blue;">namespace</span> <span style="color: #010001;">std</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">enum</span><span style="" lang="EN-US"> <span style="color: #010001;">Direction</span>{<span style="color: #010001;">rightDirection</span>,<span style="color: #010001;">zigzagUp</span>,<span style="color: #010001;">zigzagDown</span>};</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">void</span><span style="" lang="EN-US"> <span style="color: #010001;">printN</span>(<span style="color: blue;">int</span> <span style="color: #010001;">n</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">assert</span>(<span style="color: #010001;">n</span>>0);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">int</span> **<span style="color: #010001;">a</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>=<span style="color: blue;">new</span> <span style="color: blue;">int</span>*[<span style="color: #010001;">n</span>];</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> <span style="color: #010001;">i</span>=0;<span style="color: #010001;">i</span><<span style="color: #010001;">n</span>;++<span style="color: #010001;">i</span>) <span style="color: #010001;">a</span>[<span style="color: #010001;">i</span>]=<span style="color: blue;">new</span> <span style="color: blue;">int</span>[<span style="color: #010001;">n</span>];</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">int</span> <span style="color: #010001;">x</span>=0,<span style="color: #010001;">y</span>=0;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">Direction</span> <span style="color: #010001;">direction</span> = <span style="color: #010001;">rightDirection</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[0][0]=0;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> <span style="color: #010001;">i</span>=1;<span style="color: #010001;">i</span><<span style="color: #010001;">n</span>*<span style="color: #010001;">n</span>;++<span style="color: #010001;">i</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">switch</span>(<span style="color: #010001;">direction</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">case</span> <span style="color: #010001;">rightDirection</span>:</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">if</span>(<span style="color: #010001;">y</span>+1<<span style="color: #010001;">n</span>)<span style="color: #010001;">a</span>[<span style="color: #010001;">x</span>][++<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span> <span style="color: blue;">if</span>(<span style="color: #010001;">x</span>+1<0) <span style="color: #010001;">a</span>[++<span style="color: #010001;">x</span>][<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">direction</span>=<span style="color: #010001;">zigzagDown</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">break</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">case</span> <span style="color: #010001;">zigzagDown</span>:</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">if</span>(<span style="color: #010001;">x</span>+1<<span style="color: #010001;">n</span>&&<span style="color: #010001;">y</span>-1>=0)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[++<span style="color: #010001;">x</span>][--<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span> <span style="color: blue;">if</span>(<span style="color: #010001;">x</span>+1<<span style="color: #010001;">n</span>&&<span style="color: #010001;">y</span>-1<0)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[++<span style="color: #010001;">x</span>][<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">direction</span>=<span style="color: #010001;">zigzagUp</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[<span style="color: #010001;">x</span>][++<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">direction</span>=<span style="color: #010001;">zigzagUp</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">break</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">case</span> <span style="color: #010001;">zigzagUp</span>:</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">if</span> (<span style="color: #010001;">x</span>-1>=0&&<span style="color: #010001;">y</span>+1<<span style="color: #010001;">n</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[--<span style="color: #010001;">x</span>][++<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span> <span style="color: blue;">if</span> (<span style="color: #010001;">x</span>-1<0&&<span style="color: #010001;">y</span>+1<<span style="color: #010001;">n</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[<span style="color: #010001;">x</span>][++<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">direction</span>=<span style="color: #010001;">zigzagDown</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[++<span style="color: #010001;">x</span>][<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">direction</span>=<span style="color: #010001;">zigzagDown</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">break</span>;<span style=""> </span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">for</span> (<span style="color: blue;">int</span> <span style="color: #010001;">i</span>=0;<span style="color: #010001;">i</span><<span style="color: #010001;">n</span>;++<span style="color: #010001;">i</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">for</span> (<span style="color: blue;">int</span> <span style="color: #010001;">j</span>=0;<span style="color: #010001;">j</span><<span style="color: #010001;">n</span>;++<span style="color: #010001;">j</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">cout</span><<<span style="color: #010001;">setw</span>(3)<<<span style="color: #010001;">a</span>[<span style="color: #010001;">i</span>][<span style="color: #010001;">j</span>]<<<span style="color: #a31515;">','</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">cout</span><<<span style="color: #010001;">endl</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">cout</span><<<span style="color: #010001;">endl</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> <span style="color: #010001;">i</span>=0;<span style="color: #010001;">i</span><<span style="color: #010001;">n</span>;++<span style="color: #010001;">i</span>) <span style="color: blue;">delete</span>[] <span style="color: #010001;">a</span>[<span style="color: #010001;">i</span>];</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">delete</span>[] <span style="color: #010001;">a</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">int</span><span style="" lang="EN-US"> <span style="color: #010001;">_tmain</span>(<span style="color: blue;">int</span> <span style="color: #010001;">argc</span>, <span style="color: #010001;">_TCHAR</span>* <span style="color: #010001;">argv</span>[])</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(1);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(2);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(3);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(4);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(5);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(6);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">return</span> 0;</span></p>
<p><span style="" lang="EN-US">}</span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Calibri; font-size: small;"></span></span></p>
<p><span style="" lang="EN-US"></span></p>
<p><span style=""><span style="font-size: 9pt;">本人的实现思路(在《程序员面试宝典》中有另一种解法):</span><span style="" lang="EN-US"></span></span></p>
<p><span style="font-size: 9pt;"><span style="">问题的实质是把</span></span><span style="" lang="EN-US">[0,n-1]</span><span style=""><span style="font-size: 9pt;">中的整数依之字形填入一个二维数组。于是考虑按之字形遍历数组,遍历时,方向共有三个:</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">1.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">起始时向右一位:</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">a)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果没有填充完毕则向左下方斜行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">2.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">向左下方斜行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">a)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果没有碰到边界,则继续向左下方斜行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">b)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果碰到左边界而没碰到下边界;则向下直行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">c)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果碰到下边界(不管有没有碰到左边界),则向右直行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">3.<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">向右上方斜行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">a)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果没有碰到边界,则继续向右上方斜行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">b)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果碰到上边界而没有碰到右边界,向右直行;</span><span style="" lang="EN-US"></span></span></p>
<p style=""><span style="" lang="EN-US"><span style="">c)<span style='font: 7pt "Times New Roman";'> </span></span></span><span style=""><span style="font-size: 9pt;">如果碰到右边界(无论有没有碰到上边界),则向下直行;</span><span style="" lang="EN-US"></span></span></p>
<p><span style="font-size: 9pt;"><span style="">填充数字够</span></span><span style="" lang="EN-US">n*n</span><span style="font-size: 9pt;"><span style="">个时结束填充,</span></span><span style="" lang="EN-US">C++</span><span style=""><span style="font-size: 9pt;">代码如下:</span><span style="" lang="EN-US"></span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">#include</span><span style="" lang="EN-US"> <span style="color: #a31515;">"stdafx.h"</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">#include</span><span style="" lang="EN-US"> <span style="color: #a31515;"><iostream></span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">#include</span><span style="" lang="EN-US"> <span style="color: #a31515;"><iomanip></span> </span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">#include</span><span style="" lang="EN-US"> <span style="color: #a31515;"><cassert></span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">using</span><span style="" lang="EN-US"> <span style="color: blue;">namespace</span> <span style="color: #010001;">std</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">enum</span><span style="" lang="EN-US"> <span style="color: #010001;">Direction</span>{<span style="color: #010001;">rightDirection</span>,<span style="color: #010001;">zigzagUp</span>,<span style="color: #010001;">zigzagDown</span>};</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">void</span><span style="" lang="EN-US"> <span style="color: #010001;">printN</span>(<span style="color: blue;">int</span> <span style="color: #010001;">n</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">assert</span>(<span style="color: #010001;">n</span>>0);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">int</span> **<span style="color: #010001;">a</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>=<span style="color: blue;">new</span> <span style="color: blue;">int</span>*[<span style="color: #010001;">n</span>];</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> <span style="color: #010001;">i</span>=0;<span style="color: #010001;">i</span><<span style="color: #010001;">n</span>;++<span style="color: #010001;">i</span>) <span style="color: #010001;">a</span>[<span style="color: #010001;">i</span>]=<span style="color: blue;">new</span> <span style="color: blue;">int</span>[<span style="color: #010001;">n</span>];</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">int</span> <span style="color: #010001;">x</span>=0,<span style="color: #010001;">y</span>=0;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">Direction</span> <span style="color: #010001;">direction</span> = <span style="color: #010001;">rightDirection</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[0][0]=0;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> <span style="color: #010001;">i</span>=1;<span style="color: #010001;">i</span><<span style="color: #010001;">n</span>*<span style="color: #010001;">n</span>;++<span style="color: #010001;">i</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">switch</span>(<span style="color: #010001;">direction</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">case</span> <span style="color: #010001;">rightDirection</span>:</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">if</span>(<span style="color: #010001;">y</span>+1<<span style="color: #010001;">n</span>)<span style="color: #010001;">a</span>[<span style="color: #010001;">x</span>][++<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span> <span style="color: blue;">if</span>(<span style="color: #010001;">x</span>+1<0) <span style="color: #010001;">a</span>[++<span style="color: #010001;">x</span>][<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">direction</span>=<span style="color: #010001;">zigzagDown</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">break</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">case</span> <span style="color: #010001;">zigzagDown</span>:</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">if</span>(<span style="color: #010001;">x</span>+1<<span style="color: #010001;">n</span>&&<span style="color: #010001;">y</span>-1>=0)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[++<span style="color: #010001;">x</span>][--<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span> <span style="color: blue;">if</span>(<span style="color: #010001;">x</span>+1<<span style="color: #010001;">n</span>&&<span style="color: #010001;">y</span>-1<0)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[++<span style="color: #010001;">x</span>][<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">direction</span>=<span style="color: #010001;">zigzagUp</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[<span style="color: #010001;">x</span>][++<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">direction</span>=<span style="color: #010001;">zigzagUp</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">break</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">case</span> <span style="color: #010001;">zigzagUp</span>:</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">if</span> (<span style="color: #010001;">x</span>-1>=0&&<span style="color: #010001;">y</span>+1<<span style="color: #010001;">n</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[--<span style="color: #010001;">x</span>][++<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span> <span style="color: blue;">if</span> (<span style="color: #010001;">x</span>-1<0&&<span style="color: #010001;">y</span>+1<<span style="color: #010001;">n</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[<span style="color: #010001;">x</span>][++<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">direction</span>=<span style="color: #010001;">zigzagDown</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">a</span>[++<span style="color: #010001;">x</span>][<span style="color: #010001;">y</span>]=<span style="color: #010001;">i</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">direction</span>=<span style="color: #010001;">zigzagDown</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">break</span>;<span style=""> </span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">for</span> (<span style="color: blue;">int</span> <span style="color: #010001;">i</span>=0;<span style="color: #010001;">i</span><<span style="color: #010001;">n</span>;++<span style="color: #010001;">i</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">for</span> (<span style="color: blue;">int</span> <span style="color: #010001;">j</span>=0;<span style="color: #010001;">j</span><<span style="color: #010001;">n</span>;++<span style="color: #010001;">j</span>)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">cout</span><<<span style="color: #010001;">setw</span>(3)<<<span style="color: #010001;">a</span>[<span style="color: #010001;">i</span>][<span style="color: #010001;">j</span>]<<<span style="color: #a31515;">','</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">cout</span><<<span style="color: #010001;">endl</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">cout</span><<<span style="color: #010001;">endl</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">for</span>(<span style="color: blue;">int</span> <span style="color: #010001;">i</span>=0;<span style="color: #010001;">i</span><<span style="color: #010001;">n</span>;++<span style="color: #010001;">i</span>) <span style="color: blue;">delete</span>[] <span style="color: #010001;">a</span>[<span style="color: #010001;">i</span>];</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">delete</span>[] <span style="color: #010001;">a</span>;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">int</span><span style="" lang="EN-US"> <span style="color: #010001;">_tmain</span>(<span style="color: blue;">int</span> <span style="color: #010001;">argc</span>, <span style="color: #010001;">_TCHAR</span>* <span style="color: #010001;">argv</span>[])</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US">{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(1);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(2);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(3);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(4);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(5);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">printN</span>(6);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">return</span> 0;</span></p>
<p><span style="" lang="EN-US">}</span><span style="" lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Calibri; font-size: small;"></span></span></p>
相关推荐
本题是一道来自GoogleCodeJam的编程竞赛题目,主要涉及计算机科学中的数论和算法设计,特别是进制转换和二分查找技术。题目要求找出将一个十进制正整数N以全1的形式表示所需要的最小进制B。 1. **进制转换与全1表示...
- **1034**:糖果分配游戏(Candy Sharing Game),这是一道有趣的模拟题,可能涉及到游戏理论中的某些概念。 - **1056**:简单题,这类题目通常比较简单,旨在考察学生的编程基础。 - **1062**:简单的字符串处理题...
然而鲜为人知的是,在他的手稿中,竟也隐藏着一道关于趣味数学的题目。这道题目不仅体现了马克思对日常生活的观察,也显示了数学问题在解决实际问题时的魅力。 在马克思的手稿中被发现的数学问题,描述了一个简单而...
题目通过一个有趣的背景故事引入了一个数学问题。具体来说,需要求解一系列整数\( N \)对应的某个特定计算结果\( G \)。 **核心知识点:** 1. **最大公约数(Greatest Common Divisor, GCD)**:这是一种基本的数学...
接下来,我们来看一道涉及三层嵌套循环的题目。它要求找出所有可能的三位数abc和cba的组合,当它们的和为1333时,打印出a、b、c的值。通过三层嵌套循环,我们可以遍历所有可能的组合,并通过条件判断来筛选出符合...
“DiDiDi 与孪生素数”是另一道有趣的题目,它涉及到数论中的孪生素数概念。孪生素数是指一对素数,它们的差恰好为2(如(11, 13)或(17, 19))。对于这道题目,参赛者需要编写代码来判断给定的数x是否存在对应的孪生...
Scratch是一种专为儿童设计的编程语言,它通过积木块式的编程界面,使得学习编程变得简单而有趣。本压缩包“scratch10道算法题及答案.zip”提供了10个精心设计的算法题目,旨在帮助孩子们提升逻辑思维能力和问题解决...
这个资源旨在以有趣的方式介绍C++编程基础,帮助孩子们在早期阶段接触并理解编程概念。 描述中的信息与标题一致,再次确认了这个压缩包的内容是C++源代码,适合小学生学习。日期可能表示这是课程或活动的最新版本,...
- "弯管子"问题则是一道有趣的物理和数学结合的题目,它要求在保持球不动的情况下改变管子形状,体现了对实际操作和几何变换的理解。 - "蜗牛爬井"问题涉及时间管理和动态规划,要求理解蜗牛的活动模式并找到最...
考生应该仔细分析每一道题目的要求,深入思考解题思路,动手编写代码,并对编写的程序进行调试和优化。通过不断的练习和总结,考生可以加深对C语言基础知识的理解,提高编程技能,最终在考试中脱颖而出,顺利通过二...
其中,P1061“青蛙的约会”是一道经典的算法题目,考验了参赛者对动态规划和搜索算法的理解与应用。本篇将深入探讨这个问题的背景、解题思路以及C#语言下的解决方案。 题目背景: “青蛙的约会”问题源自现实生活中...
### 相关知识点 #### 1. 字符串索引访问 **知识点解析:** - 在C++中,可以通过索引直接访问字符串中的特定字符。字符串的索引是从0开始的。 - 对于字符串 `string a = ...- 该题目主要考察递归或循环算法的应用。
13. **逻辑推理**:根据帽子颜色的描述,推断帽子的数量,这是一道有趣的逻辑谜题。 14. **面积计算**:通过边长变化求面积增量,涉及面积公式及其应用。 15. **等分问题**:寻找在限制条件下的不同分法,需要掌握...
其中,“约瑟夫问题”(Josephus Problem)作为数据结构和算法中的一道经典题目,为我们提供了一个理解链表结构及迭代处理的绝佳窗口。本文将深入探讨约瑟夫问题的原理、解法以及编程实现。 约瑟夫问题的背景设定了...
前几天逛博客时看到了这样一道问题,感觉比较有趣,就自己思考了下方案顺便用python实现了一下。题目如下: 用一个二维数组表示一个简单的迷宫,用0表示通路,用1表示阻断,老鼠在每个点上可以移动相邻的东南西北四...
本篇主要介绍三道有趣的C语言试题,涉及数字运算、数据处理及几何计算。 1. **Digital Root II**: 这道题目的核心在于寻找数字的"Digital Root",即通过反复计算数字各位之和,直到结果不再变化。给定一个正整数N...
《POJ1207-The 3n + 1 problem》是北京大学在线编程平台POJ上的一道经典算法题目,其主要涉及的知识点是数论和动态规划。本题目的核心是解决著名的“Collatz Conjecture”问题,也被称为“3n+1猜想”。 3n+1猜想是...
易语言是一种专为初学者设计的编程语言,其特点...通过学习以上知识点,并结合“易语言小学试题出题源码”提供的实例,不仅可以提升编程技能,还能为小学教育提供有趣的教学资源,使学生在实践中学习和理解数学知识。
这种脚本常用于互动游戏或动画中,使得角色的行为更加生动有趣。 **7. 绘制组合图形** 此题考查的是如何使用Scratch绘制复杂图形。题目要求绘制一个由基础图形组合而成的图形,并询问了图中空白位置1处应该填写的...
- **题目背景**: 这是一道经典的数学问题,来源于斐波那契数列。问题描述为:一对兔子从出生后第3个月起每个月都会生一对兔子,而新生的小兔子在长到第3个月后也会每个月生一对兔子。假设所有兔子都不会死亡,求每个...