题解 | #坐标移动#

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

import java.util.Scanner;
import java.lang.Integer;

public class Main{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String s = sc.next();
       String[] ss = s.split(";");
       int x = 0,y = 0;
       for(int i = 0;i < ss.length;i++){
           int[] temp = adjuest(ss[i]);
           x += temp[0];
           y += temp[1];
       }
       System.out.println(x +","+y);
    }
    //判断是否有效,有效返回需要移动的坐标,无效则返回[0,0]
    public static int[] adjuest(String s){
        int[] res = new int[2];
        if(s.length() < 2) return res;
        char o = s.charAt(0);
        switch(o){
            case 'A':
                if(isDigit(s,1,s.length() - 1)){
                    res[0] = 0 - Integer.valueOf(s.substring(1,s.length()));
                }
                break;
            case 'D':
                if(isDigit(s,1,s.length() - 1)){
                    res[0] = Integer.valueOf(s.substring(1,s.length()));
                }
                break;
            case 'W':
                if(isDigit(s,1,s.length() - 1)){
                    res[1] = Integer.valueOf(s.substring(1,s.length()));
                }
                break;
            case 'S':
                if(isDigit(s,1,s.length() - 1)){
                    res[1] = 0 - Integer.valueOf(s.substring(1,s.length()));
                }
                break;
        }
        return res;
    }
    public static boolean isDigit(String s,int l,int r){//判断第二位到最后是否为纯数字
        for(int i = l;i <= r;i++){
            char c = s.charAt(i);
            if(c < '0' || c >'9') return false;
        }
        return true;
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务