紫色誓言 發表於 2014-3-4 19:47:00

c#软件工程师笔试题

<p>近来有打算重新找工作,还没提离职,投了几家公司简历,其中一家比较中意的公司给发了面试题,其实,好像是好几天前的事了,主要是Gmail邮箱很少用,所以一直都没去看,今天看到题目给解了。</p>
<p>题目如下:</p>
<h1>题目:</h1>
<p>假设我们是中国国家航天局人员,当玉兔号离开嫦娥三号之后,我们需要能够控制玉兔号在月球上开展探测工作。我们先假定虹湾区是一个很大的平原,我们在虹湾区建立一个坐标轴,如下图:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 玉兔号离开嫦娥三号后,根据自身安装的定位系统可以知道自己的初始位置,我们记为 X0 , Y0 ; 同时玉兔号也可以知道当前它的朝向,如东、西、南、北(暂时只考虑这四个方向)。</p>
<p>中国国家航天局会向玉兔号发送指令,我们先暂定为3种:</p>
<ol>
<li>F : 当玉兔号接收到这条指令之后,会向前移动一个坐标单位的距离</li>
<li>L : 当玉兔号接受到这条指令之后,会原地向左旋转90度</li>
<li>R : 当玉兔号接收到这条指令之后,会原地向右旋转90度</li>
</ol>
<h1>要求:</h1>
<p class="ListParagraph1">一)设计一个玉兔号的主程序,能够接收中国国家航天局发送过来的指令序列(如FFLFRFLL),执行该指令序列之后,玉兔号能够走到正确的位置,并知道当前正确的位置。(如:玉兔号初始位置为 (0,0),方向朝东,执行指令 FFLFRFLL之后,位置为 (3,1) 方向朝西)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p class="ListParagraph1">二)主程序中,不允许出现switch case语句,也不允许出现else关键字,也不允许使用三元表达式,if关键字出现的次数要求在5次以下(0-4次)</p>
<p class="ListParagraph1">&nbsp;</p>
<p class="ListParagraph1">三)主程序可以用任何语言编写,如Java、C#、Ruby、Python、PHP等</p>
<p class="ListParagraph1">&nbsp;</p>
<p class="ListParagraph1">四)在所选语言允许的情况下,请编写相应的单元测试&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p class="ListParagraph1">&nbsp;</p>
<p class="ListParagraph1"><span style="color: rgba(255, 0, 0, 1)">思路:一般有多条件的,我们会选择使用if/else、switch /case ,但</span><span style="color: rgba(255, 0, 0, 1)">题目明确规定 不能使用,if也限定次数,很自然就想到委托(c++里面的函数指针),还有怎么实现根据输入自动选择哪种操作,这就想到了字典(键/值).</span></p>
<p class="ListParagraph1"><span style="color: rgba(0, 0, 0, 1)">贴出代码:</span></p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">delegate</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> OperatorDelegate();
      </span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> Main(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">[] args)
      {

            </span><span style="color: rgba(0, 0, 255, 1)">string</span> instruct = <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">;

            Detector YuTu3 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Detector(<span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span>, (<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">)Diretion.East);
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> Dictory = <span style="color: rgba(0, 0, 255, 1)">new</span> System.Collections.Generic.Dictionary&lt;<span style="color: rgba(0, 0, 255, 1)">string</span>, OperatorDelegate&gt;<span style="color: rgba(0, 0, 0, 1)">();
            Dictory[</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">F</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OperatorDelegate(YuTu3.DealFont);
            Dictory[</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">L</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OperatorDelegate(YuTu3.DealLeft);
            Dictory[</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">R</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> OperatorDelegate(YuTu3.DealRight);

            </span><span style="color: rgba(0, 0, 255, 1)">while</span> (<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">exit</span><span style="color: rgba(128, 0, 0, 1)">"</span> != (instruct =<span style="color: rgba(0, 0, 0, 1)"> Console.ReadLine()))
            {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (Dictory.ContainsKey(instruct))
                {
                  Dictory();
                }
            };

            YuTu3.Print();

      }


      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">探测器</span>
      <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Detector
      {
            </span><span style="color: rgba(0, 0, 255, 1)">delegate</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> DelegateFont();
            </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> x;
            </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> y;
            </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> direction;
            </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">const</span> <span style="color: rgba(0, 0, 255, 1)">int</span> MaxDirection = <span style="color: rgba(128, 0, 128, 1)">4</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">常量表示当前有4个方向</span>
            <span style="color: rgba(0, 0, 255, 1)">private</span> Dictionary&lt;<span style="color: rgba(0, 0, 255, 1)">string</span>, DelegateFont&gt; Dictionary = <span style="color: rgba(0, 0, 255, 1)">new</span> Dictionary&lt;<span style="color: rgba(0, 0, 255, 1)">string</span>, DelegateFont&gt;<span style="color: rgba(0, 0, 0, 1)">();

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">构造函数</span>
            <span style="color: rgba(0, 0, 255, 1)">public</span> Detector(<span style="color: rgba(0, 0, 255, 1)">int</span> x, <span style="color: rgba(0, 0, 255, 1)">int</span> y, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> direction)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">this</span>.x =<span style="color: rgba(0, 0, 0, 1)"> x;
                </span><span style="color: rgba(0, 0, 255, 1)">this</span>.y =<span style="color: rgba(0, 0, 0, 1)"> y;
                </span><span style="color: rgba(0, 0, 255, 1)">this</span>.direction =<span style="color: rgba(0, 0, 0, 1)"> direction;
                Dictionary[</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DelegateFont(NorthAdd);
                Dictionary[</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DelegateFont(EastAdd);
                Dictionary[</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DelegateFont(SouthAdd);
                Dictionary[</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DelegateFont(WestAdd);
            }

            </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
            <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 逆时针
            </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> DealLeft()
            {
                direction </span>= (direction - <span style="color: rgba(128, 0, 128, 1)">1</span> + MaxDirection) %<span style="color: rgba(0, 0, 0, 1)"> MaxDirection;
               
            }

            </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
            <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 顺时针
            </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> DealRight()
            {
                direction </span>= (direction + <span style="color: rgba(128, 0, 128, 1)">1</span>) %<span style="color: rgba(0, 0, 0, 1)"> MaxDirection;
            }

            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> DealFont()
            {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">没有使用委托实现
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">if (direction == (int)Diretion.North) { ++y; return; }
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">if (direction == (int)Diretion.South) { --y; return; }
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">if (direction == (int)Diretion.West) { --x; return; }
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">++x;
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用委托+字典实现</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (Dictionary.ContainsKey(direction.ToString()))
                {
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调用委托</span>
<span style="color: rgba(0, 0, 0, 1)">                  Dictionary();
                }

            }

            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> Print()
            {
                Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">x:</span><span style="color: rgba(128, 0, 0, 1)">"</span> + x + <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">,y:</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> y);
                Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Direction:</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> (Diretion)direction);
                Console.ReadKey();
            }

            </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> NorthAdd()
            {
                </span>++<span style="color: rgba(0, 0, 0, 1)">y;
            }

            </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> SouthAdd()
            {
                </span>--<span style="color: rgba(0, 0, 0, 1)">y;
            }

            </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> WestAdd()
            {
                </span>--<span style="color: rgba(0, 0, 0, 1)">x;
            }

            </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> EastAdd()
            {
                </span>++<span style="color: rgba(0, 0, 0, 1)">x;
            }



      }



      </span><span style="color: rgba(0, 0, 255, 1)">enum</span><span style="color: rgba(0, 0, 0, 1)"> Diretion
      {
            North </span>= <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">,
            East </span>= <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">,
            South </span>= <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">,
            West </span>= <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">

      }</span></pre>
</div>
<p>使用两个委托+字典替换if/else ,switch/case,这样做的好处就是容易维护和新增数据,当代码需要添加第四种操作,第五种操作的时候,不需要改动调用的方法,只需要在探测器类里面填加操作,键值新增数据即可。</p>
<p>同时也把c#高级的委托跟字典再复习了一遍,收获颇多。不断学习,不断总结,不断成长。</p>
<p class="ListParagraph1">&nbsp;</p>
<p class="ListParagraph1">&nbsp;</p><br><br>
来源:https://www.cnblogs.com/yudeyinji/p/3581008.html
頁: [1]
查看完整版本: c#软件工程师笔试题