#include <iostream> using namespace std; const int N = 10010; struct node{ char dir; int steps; } ; int main() { node path[N]; string s; cin >> s; int cnt = 0; string tmp = ""; for (int i = 0; i < s.length(); i ++) { if (s[i] != ';') tmp += s[i]; else { for(int i = 1; i < tmp.length(); i++) { if (!(tmp[i] >= '0' && tmp[i] <= '9')) { tmp = ""; continue; } } path[cnt].dir = tmp[0]; int thisStep = 0; for (int i = 1; i < tmp.length(); i++) { thisStep *= 10; thisStep += tmp[i] - '0'; } path[cnt].steps = thisStep; cnt++; tmp = ""; } } int x = 0, y = 0; for (int i = 0; i < cnt; i++) { char thisDir = path[i].dir; switch (thisDir) { case 'W': y += path[i].steps; break; case 'A': x -= path[i].steps; break; case 'S': y -= path[i].steps; break; case 'D': x += path[i].steps; break; } } cout << x << ',' << y; }
将每个“;”之前的字符串存起来,结构化成方向和步数,过滤之后放进一个数组里,然后遍历,进行相应操作即可