题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner; import java.util.regex.Pattern; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] split = s.split(";"); //正则表达式 String ruleString1 = "[ADWS][0-9]{1,2}"; Pattern pattern = Pattern.compile(ruleString1); int x = 0, y = 0; for (String s1 : split) { if (pattern.matcher(s1).matches()) { int distance = Integer.valueOf(s1.substring(1)); char direction = s1.charAt(0); if ('A' == direction) { x -= distance; } else if ('D' ==direction) { x += distance; } else if ('W' == direction) { y += distance; } else if ('S' == direction) { y -= distance; } } } System.out.println(x+","+y); } }