<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 閱讀(404) 評論(0)  編輯  收藏 所屬分類: My Java App


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲第一页在线视频| 国产精品亚洲片在线| 亚洲精品人成网在线播放影院| 免费一区二区三区| 亚洲爆乳无码专区| 久久成人a毛片免费观看网站| 久久精品国产99精品国产亚洲性色| a级毛片无码免费真人久久| 亚洲日韩乱码中文无码蜜桃臀网站| 中文字幕一区二区三区免费视频 | 免费无码黄动漫在线观看| 亚洲卡一卡二卡乱码新区| 九九九精品成人免费视频| 亚洲乱色熟女一区二区三区蜜臀| 精品国产免费一区二区| 国产av无码专区亚洲av毛片搜| 亚洲?V乱码久久精品蜜桃| 亚洲免费日韩无码系列| 亚洲国产精品线在线观看| 久久久久久精品免费看SSS| 亚洲性色精品一区二区在线| 国产精品免费_区二区三区观看 | 拍拍拍无挡视频免费观看1000| 亚洲天堂一区二区| 成年人视频在线观看免费| 男男gay做爽爽的视频免费| 亚洲国产一区视频| 亚洲一级毛片免费观看| 久久精品国产亚洲AV未满十八| 国产自偷亚洲精品页65页| 99re6免费视频| 老牛精品亚洲成av人片| 亚洲福利在线观看| 色吊丝最新永久免费观看网站| 好湿好大好紧好爽免费视频| 亚洲精品视频在线免费| 在线观看91精品国产不卡免费| 91视频免费观看高清观看完整| 亚洲人成电影网站| 亚洲自偷自偷在线制服| 欧美大尺寸SUV免费|