题解 | #坐标移动#

坐标移动

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));
        }
    }
}

全部评论

相关推荐

小浪_Coding:1. 个人技能排版太乱, 写的技术栈太浅了, 跟测试,自动化相关的太少; 2. 项目开发类的太简单没有亮点, 算法类的项目建议只放一个,最好有自动化,CI/CD, pipline的项目, 需要更换; 3.整体排版需要优化, SOOB打招呼都需要注意等.
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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