zoukankan      html  css  js  c++  java
  • 小练习 登月机器人

      今天看到园子里有一个登月机器人的面试题,于试着写一下,有一点坑爹的地方是if不能起过5个,下面就有这种情况

    if(a){
        if(x) code1;
        if(y) code2;
    }

    这个我改成

    if(a && x) code1;
    if(a && y) code2;

    我这样不是也少了if吗?这坑爹.

    下面是一坨C#代码

        public class MoonRabbit
        {
            private string DIR = "NESW";
    
            private Tuple<int, int> position;//当前位置
            private int direction;//当前的方向
    
            public MoonRabbit(Tuple<int, int> position, char direction)
            {
                this.position = position;
                this.direction = DIR.IndexOf(direction);
            }
    
            public void Foo(string instruct)
            {
                char[] ch = instruct.ToArray<char>();
    
                for (int i = 0, ct = ch.Length; i < ct; i++)
                {
                    Rotation(ch[i]);
                    Move(ch[i]);
                }
            }
    
            public void Show()
            {
                Console.WriteLine("当前位置({0},{1}),方向{2}", position.Item1, position.Item2, DIR[direction]);
            }
    
            private void Rotation(char instruct)
            {
                if (instruct == 'R') direction = (direction + 1) % 4;
                if (instruct == 'L') direction = (direction - 1 + 4) % 4;
            }
            private void Move(char instruct)
            {
                int x = position.Item1;
                int y = position.Item2;
    
                if (instruct == 'F' && (DIR.IndexOf('E') == direction || DIR.IndexOf('W') == direction))
                    x = x + (2 - direction); //13 EW
                if (instruct == 'F' && (DIR.IndexOf('N') == direction || DIR.IndexOf('S') == direction))
                    y = y + (1 - direction);//02 NS
    
                position = new Tuple<int, int>(x, y);
            }
        }
  • 相关阅读:
    poi 导出Excel
    【EasyUI】combotree和combobox模糊查询
    多线程和Socket套接字
    io流
    前端页面的语法 jquery javascript ajax
    spring+mybatis
    Exchanger 原理
    CountDownLatch、CyclicBarrier和 Semaphore
    sleep() 、join()、yield()有什么区别
    创建线程的方式及实现
  • 原文地址:https://www.cnblogs.com/gw2010/p/3582286.html
Copyright © 2011-2022 走看看