题解 | #坐标移动#

坐标移动

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = bf.readLine()) != null) {
            String[] strings = str.split(";");
            HashMap<Integer, Integer> hp = new HashMap<>();
            hp.put(0, 0); //横轴数值
            hp.put(1, 0); //竖轴数值
            Integer count = 0;
            Integer temp = 0;
            for (int i = 0; i < strings.length; i++) {
                String string = strings[i];
                String reg = "[ADSW]\\d{1,2}";
                boolean matches = string.matches(reg);
                if (matches) {
                    if (string.startsWith("A")) {
                        String substring = string.substring(1);
                        Integer parseInt = Integer.parseInt(substring);
                        temp = hp.get(0);
                        count = temp - parseInt;
                        hp.put(0, count);
                    } else if (string.startsWith("D")) {
                        String substring = string.substring(1);
                        Integer parseInt = Integer.parseInt(substring);
                        temp = hp.get(0);
                        count = temp + parseInt;
                        hp.put(0, count);
                    } else if (string.startsWith("W")) {
                        String substring = string.substring(1);
                        Integer parseInt = Integer.parseInt(substring);
                        temp = hp.get(1);
                        count = temp + parseInt;
                        hp.put(1, count);
                    } else if (string.startsWith("S")) {
                        String substring = string.substring(1);
                        Integer parseInt = Integer.parseInt(substring);
                        temp = hp.get(1);
                        count = temp - parseInt;
                        hp.put(1, count);
                    } else {
                        continue;
                    }
                }
            }
            System.out.println(hp.get(0) + "," + hp.get(1));
        }
    }
}

全部评论

相关推荐

身边有人上海、深圳&nbsp;6、7k&nbsp;都去了,真就带薪上班了。
程序员小白条:木的办法, 以后越来越差,还是家附近宅着吧,毕业的人越来越多,岗位都提供不出来,经济又过了人口红利期
点赞 评论 收藏
分享
06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 12:10
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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