<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Scott@JAVA

    Java, 一杯濃濃的咖啡伴你到深夜

    GOOGLE挑戰賽練習題1

    Problem Statement

    A simple line drawing program uses a blank 20 x 20 pixel canvas and a directional cursor that starts at the upper left corner pointing straight down. The upper left corner of the canvas is at (0, 0) and the lower right corner is at (19, 19). You are given a string[], commands, each element of which contains one of two possible commands. A command of the form "FORWARD x" means that the cursor should move forward by x pixels. Each pixel on its path, including the start and end points, is painted black. The only other command is "LEFT", which means that the cursor should change its direction by 90 degrees counterclockwise. So, if the cursor is initially pointing straight down and it receives a single "LEFT" command, it will end up pointing straight to the right. Execute all the commands in order and return the resulting 20 x 20 pixel canvas as a string[] where character j of element i represents the pixel at (i, j). Black pixels should be represented as uppercase 'X' characters and blank pixels should be represented as '.' characters.

    Definition

    Class: DrawLines
    Method: execute
    Parameters: string[]
    Returns: string[]
    Method signature: string[] execute(string[] commands)

    (be sure your method is public)


    我的程序:

    public class DrawLines {
        
    // current cursor position
        private int xPos, yPos;

        
    private int direction;

        
    private char[][] canvas;

        
    public DrawLines() {
            xPos 
    = 0;
            yPos 
    = 0;
            
    // initial drawing direction downwards
            direction = 270;
            canvas 
    = new char[20][20];
        }


        
    private void initCanvas() {
            
    for (int i = 0; i < 20; i++)
                
    for (int j = 0; j < 20; j++)
                    canvas[i][j] 
    = '.';
        }


        
    public String[] excute(String[] commands) {
            initCanvas();
            
    for (int i = 0; i < commands.length; i++{
                
    if (commands[i].equals("LEFT")) {
                    
    // when come cross "LEFT", turn 90 degrees couter-clockwise
                    direction += 90;
                    
    if (direction == 360)
                        direction 
    = 0;
                }
     else {
                    
    int len = Integer.parseInt(commands[i].split(" ")[1]);
                    
    switch (direction) {
                    
    case 0:
                        
    // draw from left to right
                        for (int j = 0; j <= len; j++)
                            canvas[xPos][yPos
    ++= 'X';
                        yPos
    --;
                        
    break;
                    
    case 90:
                        
    // draw from down to up
                        for (int j = 0; j <= len; j++)
                            canvas[xPos
    --][yPos] = 'X';
                        xPos
    ++;
                        
    break;
                    
    case 180:
                        
    // draw from right to left
                        for (int j = 0; j <= len; j++)
                            canvas[xPos][yPos
    --= 'X';
                        yPos
    ++;
                        
    break;
                    
    case 270:
                        
    // draw from up to down
                        for (int j = 0; j <= len; j++)
                            canvas[xPos
    ++][yPos] = 'X';
                        xPos
    --;
                        
    break;
                    }

                }

            }

            String[] s 
    = new String[20];
            
    for (int i = 0; i < 20; i++)
                s[i] 
    = new String(canvas[i]);
            
    return s;
        }


        
    public static void main(String[] args) {
            String[] cmds 
    = "LEFT""FORWARD 19""LEFT""LEFT""LEFT",
                    
    "FORWARD 18""LEFT""LEFT""LEFT""FORWARD 17""LEFT",
                    
    "LEFT""LEFT""FORWARD 16""LEFT""LEFT""LEFT",
                    
    "FORWARD 15""LEFT""LEFT""LEFT""FORWARD 14""LEFT",
                    
    "LEFT""LEFT""FORWARD 13""LEFT""LEFT""LEFT",
                    
    "FORWARD 12""LEFT""LEFT""LEFT""FORWARD 11""LEFT",
                    
    "LEFT""LEFT""FORWARD 10""LEFT""LEFT""LEFT",
                    
    "FORWARD 9""LEFT""LEFT""LEFT""FORWARD 8""LEFT",
                    
    "LEFT""LEFT""FORWARD 7" }
    ;
            DrawLines drawLines 
    = new DrawLines();
            String[] s 
    = drawLines.excute(cmds);
            
    for (int i = 0; i < 20; i++)
                System.out.println(s[i]);
        }

    }

    posted on 2005-12-01 11:49 Scott@JAVA 閱讀(412) 評論(0)  編輯  收藏 所屬分類: My Java App


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 妞干网在线免费视频| 亚洲无成人网77777| 成a人片亚洲日本久久| 国产精品成人啪精品视频免费| 特黄特色的大片观看免费视频| 日本不卡免费新一二三区| 中文字幕在线日亚洲9| 美女被免费喷白浆视频| 亚洲国产日韩在线一区| 麻豆国产精品免费视频| 亚洲中文字幕无码一去台湾| 野花高清在线电影观看免费视频| 亚洲国产精品无码久久久秋霞2| 一级做a爱过程免费视频高清| 毛片免费在线观看网址| 国产亚洲国产bv网站在线| 免费精品人在线二线三线区别| 久久精品国产亚洲AV蜜臀色欲| 中文字幕亚洲综合久久2| 99久热只有精品视频免费看 | 亚洲五月综合网色九月色| 在线视频精品免费| 亚洲综合无码一区二区痴汉| 久久免费动漫品精老司机 | 亚洲精品无码专区在线在线播放 | 亚洲1234区乱码| 免费羞羞视频网站| 九九综合VA免费看| 亚洲综合色区在线观看| 97在线视频免费公开视频| 亚洲综合自拍成人| 在线精品免费视频| 九九全国免费视频| 亚洲另类图片另类电影| 四虎永久在线精品免费观看地址| 国产免费久久精品99久久| 亚洲国产精品综合久久2007| 亚洲国产成人久久精品99| 无码人妻一区二区三区免费看| 亚洲偷偷自拍高清| 伊人久久大香线蕉亚洲|