题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> #include <sstream> #include <vector> #include <string> #include <cctype> using namespace std; int main(){ int x = 0, y= 0; string in, tmp; getline(cin, in); vector<string> movements; while(in.find(";") != -1) in.replace(in.find(";"), 1," "); stringstream ss; ss << in; while(ss>>tmp) movements.push_back(tmp); //迭代移动动作 for(auto movement:movements){ if(movement.size()>3 && movement.size()<2) continue; //这一步有点取巧 else if(!isupper(movement[0]) || isalpha(movement[2])) continue; else { int m = atoi(movement.substr(1).c_str()); switch (movement[0]){ case 'A':x -= m;break; case 'D':x += m;break; case 'W':y += m;break; case 'S':y-= m;break; default: continue; } } } cout << x<<","<<y<<endl; return 0; }