题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
       String line = scanner.nextLine();
        // String line = "A10;S20;W10;D30;X;A1A;B10A11;;A10;";
        String[] split = line.split(";");

        int x = 0;
        int y = 0;

        for (int i = 0; i < split.length; i++) {


            if (split[i].length()>1){
                String location = split[i].substring(1);
                if (!location.matches(".*[a-zA-Z].*")){
                    if (split[i].startsWith("A")){
                        x -= Integer.parseInt(split[i].substring(1));
                    } else if (split[i].startsWith("D")) {
                        x += Integer.parseInt(split[i].substring(1));
                    } else if (split[i].startsWith("W")) {
                        y += Integer.parseInt(split[i].substring(1));
                    } else if (split[i].startsWith("S")) {
                        y -= Integer.parseInt(split[i].substring(1));
                    } else {
                        x = x;
                        y = y;
                    }
                }
            }


        }

        System.out.println(x+","+y);
    }

}

全部评论

相关推荐

驼瑞驰_招募评论官版...:一共经历几次握手?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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