题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <cctype> #include <iostream> #include <istream> #include <string> #include <sstream> using namespace std; bool isNum(const string &len) { for (auto item : len) { if (!isdigit(item)) return false; } return true; } void dealA(int &x, const string &len) { if (!isNum(len)) return; int move = stoi(len, 0); x -= move; } void dealD(int &x, const string &len) { if (!isNum(len)) return; int move = stoi(len, 0); x += move; } void dealW(int &y, const string &len) { if (!isNum(len)) return; int move = stoi(len, 0); y += move; } void dealS(int &y, const string &len) { if (!isNum(len)) return; int move = stoi(len, 0); y -= move; } void dealPos(int &x, int &y, string &pos) { if (pos.length() > 3 || pos.length() < 2) return; switch (pos[0]) { case 'A': dealA(x, string(pos.begin() + 1, pos.end())); break; case 'D': dealD(x, string(pos.begin() + 1, pos.end())); break; case 'W': dealW(y, string(pos.begin() + 1, pos.end())); break; case 'S': dealS(y, string(pos.begin() + 1, pos.end())); break; } } int main() { int x = 0, y = 0; string posStr, pos; cin >> posStr; istringstream iss(posStr); while (getline(iss, pos, ';')) { dealPos(x, y, pos); } cout << x << ',' << y << endl; } // 64 位输出请用 printf("%lld")