题解 | #坐标移动#

坐标移动

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

import java.io.*;
import java.util.*;
public class Main{
    public static boolean isNum(char c){
        if(c>='0'&&c<='9') return true;
        else{
            return false;
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner (System.in);
        String input = sc.nextLine();
        String[] str = input.split(";");
        int x=0,y=0;
        for(int i = 0;i<str.length;i++){
            String locstr = str[i];
            if(locstr.length()<=1 || locstr.length()>3) continue;
            char dir = locstr.charAt(0);
            String numstr = locstr.substring(1);
            int res=0,val=0;
            if ((numstr.length()==1 && isNum(numstr.charAt(0)))||(numstr.length()==2 &&isNum(numstr.charAt(0))&&isNum(numstr.charAt(1)))){
                val = Integer.parseInt(numstr); 
                switch(dir){
                    case 'A':
                        x -= val;
                        break;
                     case 'S':
                        y -= val;
                        break;
                     case 'W':
                        y += val;
                        break;
                     case 'D':
                        x += val;
                        break;
                     default:
                        x +=0;
                }
            }
        }
        System.out.println(x+","+y);
    }

}
全部评论

相关推荐

joe2333:怀念以前大家拿华为当保底的日子
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务