题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { int x=0,y=0; string str; while (cin >> str) { vector<string> steps; //拆分字符串 int wordlen = 0; for (int i = 0; i < str.length(); i++) { while (str[i] != ';') { wordlen++; i++; } steps.push_back(str.substr(i - wordlen, wordlen)); wordlen = 0; } for (int i = 0; i < steps.size(); i++) { int num = 0; if (steps[i].size() == 3) { if (steps[i][1] >= '0' && steps[i][1] <= '9' && steps[i][2] >= '0' && steps[i][2] <= '9') num = 10 * int(steps[i][1] - '0') + int(steps[i][2] - '0'); else continue; } else if (steps[i].size() == 2) { if (steps[i][1] >= '0' && steps[i][1] <= '9') num = int(steps[i][1] - '0'); else continue; } else continue; switch (steps[i][0]) { case 'A': x -= num; break; case 'D': x += num; break; case 'S': y -= num; break; case 'W': y += num; break; default: break; } } } cout<<x<<","<<y<<endl; return 0; } // 64 位输出请用 printf("%lld")