题解 | 坐标移动

坐标移动

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        int x = 0;
        int y = 0;
        String[] split = str.split(";");
        int len = split.length;
        for (int i = 0; i < len; i++) {
            if (split[i].length() != 3 && split[i].length() != 2) {
                continue;
            }
            int size = split[i].length();
            if (size == 3) {
                if (split[i].charAt(0) != 'A' && split[i].charAt(0) != 'D' &&
                        split[i].charAt(0) != 'W' && split[i].charAt(0) != 'S') {
                    continue;
                }
                if (split[i].charAt(1) < '0' || split[i].charAt(1) > '9') {
                    continue;
                }
                if (split[i].charAt(2) < '0' || split[i].charAt(2) > '9') {
                    continue;
                }
            } else {
                if (split[i].charAt(0) != 'A' && split[i].charAt(0) != 'D' &&
                        split[i].charAt(0) != 'W' && split[i].charAt(0) != 'S') {
                    continue;
                }
                if (split[i].charAt(1) < '0' || split[i].charAt(1) > '9') {
                    continue;
                }
            }
            String substring = split[i].substring(1, size);
            Integer distance = Integer.valueOf(substring);
            if (split[i].charAt(0) == 'A') {
                x -= distance;
            } else if (split[i].charAt(0) == 'D') {
                x += distance;
            } else if (split[i].charAt(0) == 'W') {
                y += distance;
            } else {
                y -= distance;
            }
        }
        System.out.print(x);
        System.out.print(',');
        System.out.print(y);
    }
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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