zoukankan      html  css  js  c++  java
  • 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#高级的委托跟字典再复习了一遍,收获颇多。不断学习,不断总结,不断成长。

     

     

  • 相关阅读:
    wpf数据验证实例及常用方法小结
    wpf自定义colorpicker
    DataGrid绑定Dictionary问题
    DataTemplate和ControlTemplate的关系
    Validation Rule和Binding Group
    WPF converter(包含传递复杂参数)
    【链接】Eclipse的Debug调试技巧
    安装apache服务出错,无法启动此程序,因为计算机中丢失VCRUNTIME140.dll
    apache解压版安装服务
    apache——(OS 10048)通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 : AH00072: make_sock: could not bind to address [::]:443
  • 原文地址:https://www.cnblogs.com/yudeyinji/p/3581008.html
Copyright © 2011-2022 走看看