题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <cctype> #include <iostream> #include <vector> #include <string> using namespace std; bool isvalued(const string& s){ if(s.size() == 1) return false; if(s.size() > 3) return false; if(s[0] != 'A' && s[0] != 'S' && s[0] != 'D' && s[0] != 'W') return false; if(!isdigit(s[1])) return false; if(s.size() == 3){ if(!isdigit(s[2])) return false; } return true; } void operation(vector<int>& pos, string& s){ int juli; if(s.size() == 2) juli = s[1] - '0'; else juli =(s[1] - '0') * 10 + s[2] - '0'; char c = s[0]; if(c == 'A') pos[0] -= juli; if(c == 'D') pos[0] += juli; if(c == 'W') pos[1] += juli; if(c == 'S') pos[1] -= juli; } int main() { vector<int> pos(2,0); string input; getline(cin, input); string ope = ""; for(auto i : input){ if(i != ';') ope += i; if(i == ';'){ if(isvalued(ope)){ operation(pos, ope); } ope.clear(); } } cout << pos[0] << ',' << pos[1] << endl; }