题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()){
            String[] inStr = sc.nextLine().split(";");
            int x = 0;
            int y = 0;
            Pattern pattern = Pattern.compile("[0-9]+");
            for (String s : inStr) {
                if(s.length() == 0){
                    continue;
                }
                String head = s.substring(0,1);
                String tail = s.substring(1);
                Matcher matcher = pattern.matcher(tail);
                if(!matcher.matches()){
                    continue;
                }
                if("W".equals(head)){
                    y += Integer.parseInt(tail);
                }else if("S".equals(head)){
                    y -= Integer.parseInt(tail);
                }else if("A".equals(head)){
                    x -= Integer.parseInt(tail);
                }else if("D".equals(head)){
                    x += Integer.parseInt(tail);
                }
            }
            System.out.println(x + "," + y);

        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
转发
投递中移(苏州)软件技术有限公司等公司10个岗位 > 牛客解忧铺
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务