题解 | #坐标移动#

坐标移动

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

这题不难,用暴力的解法仅供参考

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            String[] s = str.split(";");
            int x = 0, y = 0;
            for (int i = 0; i < s.length; i++) {
                int res = 0;
                if(s[i].length() == 3 && Character.isUpperCase(s[i].charAt(0)) && Character.isDigit(s[i].charAt(1)) && Character.isDigit(s[i].charAt(2))){
                    res = Integer.parseInt(s[i].substring(1,3));
                    if (s[i].charAt(0) == 'A'){
                        x -= res;
                    }else if (s[i].charAt(0) == 'D'){
                        x += res;
                    }else if (s[i].charAt(0) == 'W'){
                        y += res;
                    }else if (s[i].charAt(0) == 'S'){
                        y -= res;
                    }
                }else if(s[i].length() == 2 && Character.isUpperCase(s[i].charAt(0)) && Character.isDigit(s[i].charAt(1))){
                    res = Integer.parseInt(s[i].substring(1,2));
                    if (s[i].charAt(0) == 'A'){
                        x -= res;
                    }else if (s[i].charAt(0) == 'D'){
                        x += res;
                    }else if (s[i].charAt(0) == 'W'){
                        y += res;
                    }else if (s[i].charAt(0) == 'S'){
                        y -= res;
                    }
                }else{
                    x += 0;
                    y += 0;
                }
                
            }
            System.out.println(x + "," + y);
        }
    }
}
全部评论

相关推荐

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