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);
            }
        }
  • 相关阅读:
    第三第四周的笔记
    第一二周的笔记
    jQuery的一些笔记
    函数的执行环境与调用对象
    从click事件理解DOM事件流
    慕课编程题JS选项卡切换
    adb(11)-重新挂载 system 分区为可写
    adb(10)-屏幕截图/录制
    adb(9)-查看设备信息
    adb(8)-查看日志
  • 原文地址:https://www.cnblogs.com/gw2010/p/3582286.html
Copyright © 2011-2022 走看看