题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <cctype> #include <iostream> #include <string> #include <sstream> using namespace std; bool isdigit(std::string str){ for(char c : str){ if(c < 48 || c > 57) return false; }return true; } int main() { string str; cin >> str; istringstream iss(str); string move; int x = 0,y = 0; while(getline(iss,move,';')){ // cout << move << endl; if(move.length() == 2 || move.length() == 3){ if(move[0] == 'A' || move[0] == 'D' || move[0] == 'W' || move[0] == 'S'){ string dis = move.substr(1); if(isdigit(dis)){ int num = stoi(dis); // cout << move << " " << num<< endl; switch (move[0]) { case 'A':x -= num;break; case 'D':x += num;break; case 'W':y += num;break; case 'S':y -= num;break; } } } } } cout << x << "," << y << endl; } // 64 位输出请用 printf("%lld")