#include <iostream> #include <string> #include <sstream> using namespace std; bool isFakeP(const string t){ if(t.empty()) return false; if(t.length() != 2 && t.length() != 3) return false; if(t[0] != 'A' && t[0] != 'D' && t[0] != 'W' && t[0] != 'S') return false; if(t.length() == 2){ if(t[1] < '0' || t[1] > '9') return false; } if(t.length() == 3){ if(t[1] < '0' || t[1] > '9') return false; if(t[2] < '0' || t[2] > '9') return false; } return true; } void countCoordinate(const string& s){ stringstream ss(s); string t; pair<int, int>p(0, 0); while(getline(ss, t, ';')){ if(!isFakeP(t)) continue; int n = stoi(t.substr(1)); switch(t[0]){ case'A' : p.first -= n; break; case'D' : p.first += n; break; case'W' : p.second += n; break; case'S' : p.second -= n; break; } } cout << p.first << ',' << p.second << endl; } int main() { string s; getline(cin, s); countCoordinate(s); return 0; } // 64 位输出请用 printf("%lld")
计算移动的子函数+判断坐标是否有效的子函数
问题点:
1.坐标中的数值获得,用substr截取;
2.stringstream字符流用来识别“;”分割字符串分别处理;
3.switch用来多重判断,记得break