题解 | 坐标移动

坐标移动

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

import java.util.*;
import java.util.regex.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] stss = in.nextLine().split(";");
        int sumY = 0, sumX = 0;
        for (String e : stss) {
            Pattern pattern = Pattern.compile("[ASWD]([1-9]\\d?)");
            Matcher matcher = pattern.matcher(e);
            if (matcher.matches()) {
                char chc = e.charAt(0);
                int ini = Integer.parseInt(e.substring(1));
                if (e.charAt(0) == 'A') {
                    sumX -= ini;
                } else if (e.charAt(0) == 'D') {
                    sumX += ini;
                } else if (e.charAt(0) == 'W') {
                    sumY += ini;
                } else if (e.charAt(0) == 'S') {
                    sumY -= ini;
                }
            }

        }
        System.out.print(sumX + "," + sumY);

    }
}

全部评论

相关推荐

coffrar:全都是已读😅沟通一千五百多个了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务