题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> using namespace std; bool isNum(string s) { int n = s.size(); for(int i = 0; i < n; i++) { if(!isdigit(s[i])) return false; } return true; } int main() { string s; int x = 0, y = 0; while (getline(cin, s, ';')) { if (s.empty())continue; int len = s.size(); string s1 = s.substr(1); if(isNum(s1)) { switch(s[0]) { case'A': x -= stoi(s1);break; case'D': x += stoi(s1);break; case'W': y += stoi(s1);break; case'S': y -= stoi(s1);break; deflaut: break; } } } cout << x << ',' << y << endl; } // int main() { // string s; // int x = 0, y = 0; // while (getline(cin, s, ';')) { // if (s.empty())continue; // int len = s.size(); // if (len == 2 && s[0] == 'A' && isdigit(s[1]))x -= s[1] -'0'; // if (len == 2 && s[0] == 'D' && isdigit(s[1]))x += s[1] -'0'; // if (len == 2 && s[0] == 'W' && isdigit(s[1]))y += s[1] -'0'; // if (len == 2 && s[0] == 'S' && isdigit(s[1]))y -= s[1] -'0'; // if (len == 3 && s[0] == 'A' && isdigit(s[1]) && isdigit(s[2]))x -= stoi(s.substr(1)); // if (len == 3 && s[0] == 'D' && isdigit(s[1]) && isdigit(s[2]))x += stoi(s.substr(1)); // if (len == 3 && s[0] == 'W' && isdigit(s[1]) && isdigit(s[2]))y += stoi(s.substr(1)); // if (len == 3 && s[0] == 'S' && isdigit(s[1]) && isdigit(s[2]))y -= stoi(s.substr(1)); // } // cout << x << ',' << y << endl; // }