题解 | #坐标移动#

坐标移动

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

模拟

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);

        int x = 0, y = 0;
        String[] instructions = br.readLine().split(";");
        for (String instruct : instructions) {
            int n = instruct.length();
            if (n < 2 || n > 3) {
                continue;
            }
            // 第一个字符是方向
            char dir = instruct.charAt(0);
            // 后面的是距离
            int dist = 0;
            for (int i = 1; i < n; i++) {
                char c = instruct.charAt(i);
                if (!Character.isDigit(c)) {
                    dist = 0;
                    break;
                }
                dist = dist * 10 + c - '0';
            }
            switch (dir) {
                case 'W':
                    y += dist;
                    break;
                case 'A':
                    x -= dist;
                    break;
                case 'S':
                    y -= dist;
                    break;
                case 'D':
                    x += dist;
                    break;
                default:
                    break;
            }
        }
        pw.printf("%d,%d\n", x, y);

        pw.flush();
        pw.close();
        br.close();
    }
}
全部评论

相关推荐

喜欢吃蛋糕仰泳鲈鱼是我的神:字节可以找个hr 给你挂了,再放池子捞
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务