题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args)throws Exception{
Scanner sc=new Scanner(System.in);
String[] str = sc.nextLine().split(";");
int x=0;
int y=0;
HashMap<Character,Integer> hmap = new HashMap<>();
hmap.put('A',-10);hmap.put('D',10);hmap.put('S',-10);hmap.put('W',10);
for(int i=0;i<str.length;i++){
char[] ch=str[i].toCharArray();
int len=ch.length;
if(len>1 && hmap.containsKey(ch[0])){
if(len==2 && Character.isDigit(ch[1])){
int t=(int) ch[1]-'0';
switch(ch[0]){
case 'A': x -= t;break;
case 'D': x += t;break;
case 'S': y -= t;break;
case 'W': y += t;break;
}
}
if(len==3 &&Character.isDigit(ch[1]) &&Character.isDigit(ch[2])){
int t0=(int) ch[1]-'0';
int t1=(int) ch[2]-'0';
int t=(int) 10*t0+t1;
switch(ch[0]){
case 'A': x -= t;break;
case 'D': x += t;break;
case 'S': y -= t;break;
case 'W': y += t;break;
}
}
}
else continue;
}
System.out.println(x+","+y);
}
}
import java.util.*;
public class Main{
public static void main(String[] args)throws Exception{
Scanner sc=new Scanner(System.in);
String[] str = sc.nextLine().split(";");
int x=0;
int y=0;
HashMap<Character,Integer> hmap = new HashMap<>();
hmap.put('A',-10);hmap.put('D',10);hmap.put('S',-10);hmap.put('W',10);
for(int i=0;i<str.length;i++){
char[] ch=str[i].toCharArray();
int len=ch.length;
if(len>1 && hmap.containsKey(ch[0])){
if(len==2 && Character.isDigit(ch[1])){
int t=(int) ch[1]-'0';
switch(ch[0]){
case 'A': x -= t;break;
case 'D': x += t;break;
case 'S': y -= t;break;
case 'W': y += t;break;
}
}
if(len==3 &&Character.isDigit(ch[1]) &&Character.isDigit(ch[2])){
int t0=(int) ch[1]-'0';
int t1=(int) ch[2]-'0';
int t=(int) 10*t0+t1;
switch(ch[0]){
case 'A': x -= t;break;
case 'D': x += t;break;
case 'S': y -= t;break;
case 'W': y += t;break;
}
}
}
else continue;
}
System.out.println(x+","+y);
}
}