题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*;
import java.util.regex.*;
import java.util.stream.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
System.out.println(positionXY(in.nextLine()));
}
}
/**
* HJ17 坐标移动
*
* @param str 多个有效无效坐标拼接的字符串
* @return 最终坐标
*/
private static String positionXY(String str) {
int x = 0;
int y = 0;
List<String> lst = Arrays.stream(str.split(";"))
.filter(s -> Pattern.matches("^(A|S|W|D)[0-9]{1,2}$", s))
.collect(Collectors.toList());
for (String s : lst) {
char c = s.charAt(0);
switch (c){
case 'A':
x = x - getNum(s, c);
break;
case 'D':
x = x + getNum(s, c);
break;
case 'W':
y = y + getNum(s, c);
break;
case 'S':
y = y - getNum(s, c);
break;
default:
break;
}
}
return x + "," + y;
}
private static int getNum(String s, char c) {
return Integer.parseInt(s.replace(c, ' ').trim());
}
}
三奇智元机器人科技有限公司公司福利 88人发布