题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*;
import java.util.stream.*;
import java.util.regex.Pattern;
// 注意类名必须为 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
Map<String,Integer> map=new HashMap();
String a = in.nextLine();
String[] arr = a.split(";");
Arrays.asList(arr)
.stream()
.filter(item-> {
String reg = "^[A,D,S,W]\\d{1,2}$";
boolean a10 = Pattern.matches(reg, item);
return a10;
}).forEach(item->{
String key=item.substring(0,1);
Integer count=Integer.valueOf(item.substring(1));
Integer value=map.getOrDefault(key,0);
map.put(key,value+count);
});
Integer[] x={0},y={0};
map.entrySet().forEach(item->{
String key=item.getKey();
Integer count=item.getValue();
if(key.equals("A")){
x[0]-=count;
}
if(key.equals("D")){
x[0]+=count;
}
if(key.equals("S")){
y[0]-=count;
}
if(key.equals("W")){
y[0]+=count;
}
});
String result=String.format("%d,%d",x[0],y[0]);
System.out.println(result);
}
}
}

查看7道真题和解析