题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> #include <string> using namespace std; int main() { int row = 0; int col = 0;//分别表示横纵坐标 string str; cin >> str; string curStr; for(int i = 0; i < str.length(); i++){ if(str[i] != ';') curStr += str[i]; else if((curStr.length() == 3 && curStr[1] >= '0' && curStr[1] <= '9' && curStr[2] >= '0' && curStr[2] <= '9') || (curStr.length() == 2 && curStr[1] >= '0' && curStr[1] <= '9')){ int stepLen = stoi(curStr.substr(1, 2)); switch (curStr[0]) { case 'W' : col += stepLen; break; case 'S' : col -= stepLen; break; case 'A' : row -= stepLen; break; case 'D' : row += stepLen; break; default: break; } curStr.clear(); } else curStr.clear(); } cout << row << "," << col << endl; return 0; }