华为在线编程系列-坐标移动

来源:互联网 发布:芮时原创女装 知乎 编辑:程序博客网 时间:2024/04/30 18:31

题目描述

开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。

输入:

合法坐标为A(或者D或者W或者S) + 数字(两位以内)

坐标之间以;分隔。

非法坐标点需要进行丢弃。如AA10; A1A; ; YAD; 等。

下面是一个简单的例子 如:

A10;S20;W10;D30;X;A1A;B10A11;;A10;

处理过程:

起点(0,0)

  • A10 = (-10,0)

  • S20 = (-10,-20)

  • W10 = (-10,-10)

  • D30 = (20,-10)

  • x = 无效

  • A1A = 无效

  • B10A11 = 无效

  • 一个空 不影响

  • A10 = (10,-10)

结果 (10, -10)

我的思路:首先写一个方法来判断输入的该字符串是否有效,如果有效在分四种情况对横坐标和纵坐标进行移动

牛客上没有AC但是IDE输出结果正确(牛客不输出东西)

import java.util.Scanner;/** * Created by hmh on 2017/4/1. * 坐标移动(在牛客平台不能通过) */public class MovePosition {    public static void main(String[] args){        Scanner scanner = new Scanner(System.in);        String str = scanner.nextLine();        String[] arr = str.split(";");        int[][] result = new int[1][2];        for(int i = 0; i < arr.length; i++){            if(isValid(arr[i])){                switch (arr[i].charAt(0)){                    case 'A':                        result[0][0] -= Integer.parseInt(arr[i].substring(1));                        break;                    case 'D':                        result[0][0] += Integer.parseInt(arr[i].substring(1));                        break;                    case 'W':                        result[0][1] += Integer.parseInt(arr[i].substring(1));                        break;                    case 'S':                        result[0][1] -= Integer.parseInt(arr[i].substring(1));                        break;                }            }else{                continue;            }        }        System.out.print(result[0][0] + "," +result[0][1]);    }    public static boolean isValid(String string){        if(string.equals("") || string == null){            return false;        }        if(string.length() > 3){            return false;        }        if (string.toUpperCase().charAt(0) <= 'Z' && string.toUpperCase().charAt(0) >= 'A') {            for(int i = 1; i < string.length(); i++){                if(string.charAt(i) > '9' || string.charAt(i) < '0'){                    return false;                }            }        }        return true;    }}

更新!!!!!!
这次终于AC了,很多情况下,在牛客上跑的程序没有输出,是因为测试用例的输入问题(下次遇到IDE里运行正确,而牛客网没有内容输出,就可以试一下System.out.println()或者加上scanner.hasNextLine的判断)

import java.util.Scanner;/** * Created by hmh on 2017/4/1. * */public class MovePosition {    public static void main(String[] args){        Scanner scanner = new Scanner(System.in);        //String str = "";        while(scanner.hasNextLine()){            String str = scanner.nextLine();            String[] arr = str.split(";");           // int[][] result = new int[1][2];            int x = 0;            int y = 0;            for(int i = 0; i < arr.length; i++){                if(isValid(arr[i])){                    switch (arr[i].charAt(0)){                        case 'A':                            x -= Integer.parseInt(arr[i].substring(1));                            break;                        case 'D':                            x += Integer.parseInt(arr[i].substring(1));                            break;                        case 'W':                            y += Integer.parseInt(arr[i].substring(1));                            break;                        case 'S':                            y -= Integer.parseInt(arr[i].substring(1));                            break;                    }                }else{                    continue;                }            }            System.out.println(x + "," + y);        }    }    public static boolean isValid(String string){        if(string.equals("") || string == null){            return false;        }        if(string.length() > 3){            return false;        }        if (string.toUpperCase().charAt(0) <= 'Z' && string.toUpperCase().charAt(0) >= 'A') {            for(int i = 1; i < string.length(); i++){                if(string.charAt(i) > '9' || string.charAt(i) < '0'){                    return false;                }            }        }        return true;    }}
0 0