题解 | #坐标移动# 不太像中等的题

坐标移动

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 scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");

        String s1 = null;
        if (scanner.hasNext()) {
            s1 = scanner.next();
        }
        int X = 0;
        int Y = 0;
        String[] split = s1.split(";");
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            if (pipei(s)) {
                if (s.charAt(0) == 'W') {
                    Y += Integer.parseInt(s.substring(1));
                } else if (s.charAt(0) == 'D') {
                    X += Integer.parseInt(s.substring(1));
                } else if (s.charAt(0) == 'S') {
                    Y -= Integer.parseInt(s.substring(1));
                } else if (s.charAt(0) == 'A') {
                    X -= Integer.parseInt(s.substring(1));
                }
            }
        }
        System.out.println(X + "," + Y);
    }

    public static boolean pipei(String s2) {
        Pattern compile = Pattern.compile("[WASD][0-9]{1,2}");
        Matcher matcher = compile.matcher(s2);
        return matcher.matches();
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务