题解 | #坐标移动#

坐标移动

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

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

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            String reg = "(A|S|W|D)([0-9]+)";
            String ss[] = a.split(";");
            ArrayList<String> al = new ArrayList<>();
            for (int i = 0; i < ss.length; i++) {
                if (Pattern.matches(reg, ss[i])) {
                    al.add(ss[i]);
                }
            }
            int[] res = {0, 0};
            for (int i = 0; i < al.size(); i++) {
                String f = al.get(i).substring(0, 1);
                String st = al.get(i).substring(1);
                int n = Integer.parseInt(st);
                if (f.equals("A")) {
                    res[0] -= n;
                } else if (f.equals("D")) {
                    res[0] += n;
                } else if (f.equals("W")) {
                    res[1] += n;
                } else if (f.equals("S")) {
                    res[1] -= n;
                }
            }
            System.out.println(res[0] + "," + res[1]);
        }
    }
}

全部评论

相关推荐

09-19 13:59
门头沟学院 Java
用微笑面对困难:Trae一下,如果真成了,他用了直接发字节起诉代码版权,,这个代码不商用是没问题的如果没成也是情理之中的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务