题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] strs = br.readLine().trim().split(";"); Map<Character, Integer> map = new HashMap<>(); for(int i = 0; i < strs.length; i++){ String curStr = strs[i]; if(curStr.length() < 2 || curStr.length() > 3 ){ continue; } char dirc = curStr.charAt(0); if(isValid(dirc)){ if(curStr.length() == 2){ char sec = curStr.charAt(1); if(sec >= '0' && sec <= '9' ){ int secInt = sec - '0'; if(map.containsKey(dirc)){ map.put(dirc, (map.get(dirc)+secInt)); }else{ map.put(dirc, secInt); } }else{ continue; } }else if (curStr.length() == 3){ char sec = curStr.charAt(1); int secInt = 0, thiInt = 0; if(sec >= '0' && sec <= '9' ){ secInt = sec - '0'; }else{ continue; } char thi = curStr.charAt(2); if(thi >= '0' && thi <= '9' ){ thiInt = thi - '0'; }else{ continue; } int num = secInt * 10 + thiInt; if(map.containsKey(dirc)){ map.put(dirc, (map.get(dirc)+num)); }else{ map.put(dirc, num); } } } } int x = 0, y = 0; for(Map.Entry<Character, Integer> entry : map.entrySet()){ if(entry.getKey() == 'A'){ x = x - entry.getValue(); }else if(entry.getKey() == 'D'){ x = x + entry.getValue(); }else if(entry.getKey() == 'W'){ y = y + entry.getValue(); }else{ y = y - entry.getValue(); } } System.out.print(x+","+y); } public static boolean isValid(char ch){ return (ch == 'A' || ch == 'D' || ch == 'W' || ch == 'S'); } }