题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List list = new ArrayList<String>();
try {
String a = in.nextLine();
String[] str = a.split(";");
for (int i = 0; i < str.length; i++) {
if (str[i].matches("[A-Z][0-9][0-9]")) {
list.add(str[i]);
}
}
int x = 0, y = 0 ; //坐标
for (String st : str) {
if (st.length() < 2 || !(st.substring(1).matches("[0-9][0-9]") ||
st.substring(1).matches("[0-9]")) ) {
continue;
}
String leap = st.substring(0, 1);
int n = Integer.valueOf(st.substring(1));
switch (leap) {
//向左移动
case "A":
x = x - n;
break;
//向右移动
case "D":
x = x + n;
break;
//向上移动
case "W":
y = y + n;
break;
//向下移动
case "S":
y = y - n;
break;
}
}
System.out.print(x + "," + y);
} catch (InputMismatchException e) {
System.out.print("你的输入有误!");
} finally {
if (in != null) {
in.close();
}
}
}
}
查看18道真题和解析
