题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
不用正则表达式
#include<iostream> #include<sstream> #include<string> #include<vector> using namespace std; void dearCoordinate(int& x, int& y, string mv){ if(mv.size()==0) return; int cnt(0); for(auto item:mv){ if((item>'9'||item < '0') && item!='A'&&item!='S'&&item!='W'&&item!='D') return; if(item=='A'||item=='S'||item=='W'||item=='D') { if(++cnt>=2) return; } } char mvS = mv[0]; stringstream ss(mv.substr(1, mv.length()-1)); int step; ss>>step; switch(mvS){ case 'A': x-=step; break; case 'D': x+=step; break; case 'W': y+=step; break; case 'S': y-=step; break; } } int main(){ string str; cin>>str; vector<string> steps; for(auto& item:str){ if(item==';'){ item = ' '; } } stringstream ss(str); int x(0),y(0); while(ss){ string temp; ss>>temp; dearCoordinate(x, y, temp); } cout<<x<<','<<y<<endl; }