题解 | #坐标移动# 只需要正则(零宽断言)切割一次
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*; import java.io.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main{ public static void main(String args[]){ Scanner s=new Scanner(System.in); String str=s.nextLine(); Pattern p=Pattern.compile("(?<=^|;)[WASD]\\d+(?=;)"); Matcher m=p.matcher(str); int x=0,y=0; while(m.find()){ //System.out.println(m.group()); String cord=m.group(); int num=0; for(int i=1;i<cord.length();i++){ num=num*10+cord.charAt(i)-'0'; } switch(cord.charAt(0)){ case 'W': y+=num; break; case 'A': x-=num; break; case 'S': y-=num; break; case 'D': x+=num; break; } } System.out.println(x+","+y); } }