题解 | #坐标移动#
坐标移动
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); } } } }