题解 | #HJ17 坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <bits/stdc++.h> using namespace std; unordered_set<char> st; int check(string str) { if (str.size() > 3 || str.size() == 0) return -1; if (st.count(str[0]) == 0) return -1; if (str.size() == 2) { if (!isdigit(str[1])) return -1; return str[1] - '0'; } if (str.size() == 3) { if (isdigit(str[1]) && isdigit(str[2])) { return stoi(str.substr(1, 2)); } else return -1; } return -1; } int main() { int x = 0, y = 0; st.insert('A'); st.insert('W'); st.insert('S'); st.insert('D'); string s; int index = 0; getline(cin, s); for (int i = 0; i < s.size(); i++) { if (s[i] == ';') { string temp = s.substr(index, i - index); int res = check(temp); if (res != -1) { if (temp[0] == 'A') x -= res; if (temp[0] == 'D') x += res; if (temp[0] == 'S') y -= res; if (temp[0] == 'W') y += res; } index = i + 1; } } cout << x << "," << y; return 0; }