题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> #include <vector> #include<string> using namespace std; bool check_cmd(string& cmd){ if (cmd.size()>1&&cmd.size()<=3){ char dir= cmd[0]; if(dir!='A' && dir!='D' && dir!='S' && dir!='W') return false; for(int i=1; i<cmd.size(); ++i) { if(cmd[i]<'0' || cmd[i]>'9'){ return false; } } return true; } return false; } int str_2_int(string& a){ int num=0; for(int i=0; i<a.size(); ++i){ num = num*10+a[i]-'0'; } return num; } int main() { vector<string> command; string cmd_prt; while(getline(cin, cmd_prt,';')){ if(cmd_prt.empty())continue; if(check_cmd(cmd_prt)) command.push_back(cmd_prt); } // pair<int, int> position(0,0); for(int i=0; i<command.size(); ++i){ string buff =command[i]; char move=buff[0]; string buff2 = buff.substr(1,buff.size()-1); int stride = str_2_int(buff2); if(move=='A'){position.first -= stride;} else if (move=='D') {position.first += stride;} else if (move=='W') {position.second += stride;} else{position.second -= stride;} } cout<<position.first<<","<<position.second; // } // 64 位输出请用 printf("%lld")