题解 | #坐标移动#

坐标移动

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

import java.util.Scanner; 

import java.util.regex.Pattern;
import java.util.regex.Matcher;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        if (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.next();
            if(str != null && str.length() >0 && str.length() <= 10000) {
                Pattern pat = Pattern.compile("^[A|W|S|D][1-9][0-9]{0,1}$");
                String[] strArr = str.split(";");
                int x = 0;
                int y = 0;
                for(String newStr : strArr) {
                    Matcher mt = pat.matcher(newStr);
                    if(mt.find()) {
                       if(newStr.substring(0,1).equals("A")) {
                         x -= Integer.valueOf(newStr.substring(1));
                       } 
                       if(newStr.substring(0,1).equals("D")) {
                         x += Integer.valueOf(newStr.substring(1));
                       } 
                       if(newStr.substring(0,1).equals("S")) {
                         y -= Integer.valueOf(newStr.substring(1));
                       } 
                       if(newStr.substring(0,1).equals("W")) {
                         y += Integer.valueOf(newStr.substring(1));
                       } 
                    }
                }
                System.out.print( x + "," + y);
            }
        }
    }
}

全部评论

相关推荐

allin秋招的单身...:我投过这家 上来就发个设计图给我,让我做好发到他邮箱
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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