题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String a = in.nextLine();
String reg = "(A|S|W|D)([0-9]+)";
String ss[] = a.split(";");
ArrayList<String> al = new ArrayList<>();
for (int i = 0; i < ss.length; i++) {
if (Pattern.matches(reg, ss[i])) {
al.add(ss[i]);
}
}
int[] res = {0, 0};
for (int i = 0; i < al.size(); i++) {
String f = al.get(i).substring(0, 1);
String st = al.get(i).substring(1);
int n = Integer.parseInt(st);
if (f.equals("A")) {
res[0] -= n;
} else if (f.equals("D")) {
res[0] += n;
} else if (f.equals("W")) {
res[1] += n;
} else if (f.equals("S")) {
res[1] -= n;
}
}
System.out.println(res[0] + "," + res[1]);
}
}
}
查看10道真题和解析