题解 | 坐标移动
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> using namespace std; bool is_num(string s) { for (char c : s) { if ( c < '0' || c > '9') { return false; } } return true; } int main() { string str; pair<int, int>p(0, 0); while (getline(cin, str, ';')) { if (str.empty()) continue; string s1 = str.substr(1); if (is_num(s1)) { switch (str[0]) { case 'A': p.first -= stoi(s1); break; case 'D': p.first += stoi(s1); break; case 'W': p.second += stoi(s1); break; case 'S': p.second -= stoi(s1); break; default: break; } } } cout << p.first <<"," << p.second; return 0; } // 64 位输出请用 printf("%lld")
getline(cin, str, ';') 这个函数能够自定义终止符,同时stoi和aiot这两个函数负责字符和数字之间的转换;