题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include<bits/stdc++.h> using namespace std; vector<string> split(char c,string src){ vector<string> res; int sp =0, fp = 0; while(fp <src.length()){ fp = src.find(c,sp); res.push_back(src.substr(sp,fp-sp)); sp = fp + 1; } return res; } bool ok(string s){ string dir = "ASDW"; int len = s.length(); if(len>3) return false; bool ok1 = s.length() && dir.find(s[0]) < dir.length(); if(len == 2){ return ok1 && s[1] >='1' && s[1] <='9'; } if(len == 3){ return ok1 && s[1] >='1' && s[1] <='9' && s[2]>='0' && s[2] <='9'; } return false; } int main(){ string s; cin>>s; int x=0,y=0; vector<string> ss = split(';', s); for(int i=0;i<ss.size();i++){ //cout<<"ss[i] "<<ss[i]<<endl; if(ok(ss[i])){ //cout<<"ok ss[i] "<<ss[i]<<endl; string s_num = ss[i].substr(1); int dis = 0; for(int j=0;j<s_num.length();j++){ dis *=10; dis += s_num[j] -'0'; } if(ss[i][0] == 'A'){ x -= dis; }else if(ss[i][0] == 'S'){ y -= dis; }else if(ss[i][0] == 'W'){ y += dis; }else if(ss[i][0] == 'D'){ x += dis; } } } cout<<x<<','<<y<<endl; return 0; }