先用 stringstream 分隔分号 ; 得到每次操作的指令字符串 t ,然后 regex_match 全文匹配 t 的第一个字符后是否全为数字,接着由 t 的第一个字符判断此次操作的移动方向来对 pair 记录的坐标(0,0)进行操作。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
string s,t;
while(getline(cin,s)) {
stringstream ss(s);
pair<int,int> p(0,0);
while(getline(ss,t,';')) {
if(t.empty())
continue;
string _ = t.substr(1);
if(regex_match(_,regex("[0-9]*"))) {
switch(t[0]) {
case 'A': p.first -= stoi(_); break; //左移
case 'D': p.first += stoi(_); break; //右移
case 'W': p.second += stoi(_); break; //上移
case 'S': p.second -= stoi(_); break; //下移
default: break; //无效
}
}
}
cout << p.first << "," << p.second << endl;
}
return 0;
}
京公网安备 11010502036488号