题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
感这题还是比较简单的,考察正则表达式的应用以及字符串处理,Java 类库熟悉的话,很快就能完成。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String line = in.next();
String[] ops = line.split(";");
int x = 0;
int y = 0;
for(String op : ops) {
if(op.matches("[WASD][1-9]?[0-9]")) {
int move = Integer.parseInt(op.substring(1));
switch(op.charAt(0)) {
case 'W': y += move;break;
case 'A': x -= move;break;
case 'S': y -= move;break;
case 'D': x += move;break;
}
}
}
System.out.println(x + "," + y);
}
}
}
科大讯飞公司氛围 477人发布