c#軟件工程師筆試題


近來有打算重新找工作,還沒提離職,投了幾家公司簡歷,其中一家比較中意的公司給發了面試題,其實,好像是好幾天前的事了,主要是Gmail郵箱很少用,所以一直都沒去看,今天看到題目給解了。

題目如下:

題目:

假設我們是中國國家航天局人員,當玉兔號離開嫦娥三號之后,我們需要能夠控制玉兔號在月球上開展探測工作。我們先假定虹灣區是一個很大的平原,我們在虹灣區建立一個坐標軸,如下圖:

         玉兔號離開嫦娥三號后,根據自身安裝的定位系統可以知道自己的初始位置,我們記為 X0 , Y0 ; 同時玉兔號也可以知道當前它的朝向,如東、西、南、北(暫時只考慮這四個方向)。

中國國家航天局會向玉兔號發送指令,我們先暫定為3種:

  1. F : 當玉兔號接收到這條指令之后,會向前移動一個坐標單位的距離
  2. L : 當玉兔號接受到這條指令之后,會原地向左旋轉90度
  3. R : 當玉兔號接收到這條指令之后,會原地向右旋轉90度

要求:

一)設計一個玉兔號的主程序,能夠接收中國國家航天局發送過來的指令序列(如FFLFRFLL),執行該指令序列之后,玉兔號能夠走到正確的位置,並知道當前正確的位置。(如:玉兔號初始位置為 (0,0),方向朝東,執行指令 FFLFRFLL之后,位置為 (3,1) 方向朝西)

        

二)主程序中,不允許出現switch case語句,也不允許出現else關鍵字,也不允許使用三元表達式,if關鍵字出現的次數要求在5次以下(0-4次)

 

三)主程序可以用任何語言編寫,如Java、C#、Ruby、Python、PHP等

 

四)在所選語言允許的情況下,請編寫相應的單元測試         

 

思路:一般有多條件的,我們會選擇使用if/else、switch /case ,但題目明確規定 不能使用,if也限定次數,很自然就想到委托(c++里面的函數指針),還有怎么實現根據輸入自動選擇哪種操作,這就想到了字典(鍵/值).

貼出代碼:

 public delegate void OperatorDelegate();
        static void Main(string[] args)
        {

            string instruct = "";

            Detector YuTu3 = new Detector(0, 0, (int)Diretion.East);
            var Dictory = new System.Collections.Generic.Dictionary<string, OperatorDelegate>();
            Dictory["F"] = new OperatorDelegate(YuTu3.DealFont);
            Dictory["L"] = new OperatorDelegate(YuTu3.DealLeft);
            Dictory["R"] = new OperatorDelegate(YuTu3.DealRight);

            while ("exit" != (instruct = Console.ReadLine()))
            {
                if (Dictory.ContainsKey(instruct))
                {
                    Dictory[instruct]();
                }
            };

            YuTu3.Print();

        }


        //探測器
        class Detector
        {
            delegate void DelegateFont();
            private int x;
            private int y;
            private int direction;
            private const int MaxDirection = 4;//常量表示當前有4個方向
            private Dictionary<string, DelegateFont> Dictionary = new Dictionary<string, DelegateFont>();

            //構造函數
            public Detector(int x, int y, int direction)
            {
                this.x = x;
                this.y = y;
                this.direction = direction;
                Dictionary["0"] = new DelegateFont(NorthAdd);
                Dictionary["1"] = new DelegateFont(EastAdd);
                Dictionary["2"] = new DelegateFont(SouthAdd);
                Dictionary["3"] = new DelegateFont(WestAdd);
            }

            /// <summary>
            /// 逆時針
            /// </summary>
            public void DealLeft()
            {
                direction = (direction - 1 + MaxDirection) % MaxDirection;
               
            }

            /// <summary>
            /// 順時針
            /// </summary>
            public void DealRight()
            {
                direction = (direction + 1) % MaxDirection;
            }

            public void DealFont()
            {
                //沒有使用委托實現
                //if (direction == (int)Diretion.North) { ++y; return; }
                //if (direction == (int)Diretion.South) { --y; return; }
                //if (direction == (int)Diretion.West) { --x; return; }
                //++x;
                //使用委托+字典實現
                if (Dictionary.ContainsKey(direction.ToString()))
                {
                    //調用委托
                    Dictionary[direction.ToString()]();
                }

            }

            public void Print()
            {
                Console.WriteLine("x:" + x + ",y:" + y);
                Console.WriteLine("Direction:" + (Diretion)direction);
                Console.ReadKey();
            }

            private void NorthAdd()
            {
                ++y;
            }

            private void SouthAdd()
            {
                --y;
            }

            private void WestAdd()
            {
                --x;
            }

            private void EastAdd()
            {
                ++x;
            }



        }



        enum Diretion
        {
            North = 0,
            East = 1,
            South = 2,
            West = 3

        }

使用兩個委托+字典替換if/else ,switch/case,這樣做的好處就是容易維護和新增數據,當代碼需要添加第四種操作,第五種操作的時候,不需要改動調用的方法,只需要在探測器類里面填加操作,鍵值新增數據即可。

同時也把c#高級的委托跟字典再復習了一遍,收獲頗多。不斷學習,不斷總結,不斷成長。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM