本题主要考察字符串的处理,而c++没有Java中的split函数,所以造成一定的困扰。 这里我分享给大家一个高可用性的split函数。
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
static void split(const string& s, vector<string>& tokens, const string& delimiters = " ")
{
string::size_type lastPos = s.find_first_not_of(delimiters, 0);
string::size_type pos = s.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos) {
tokens.push_back(s.substr(lastPos, pos - lastPos));
lastPos = s.find_first_not_of(delimiters, pos);
pos = s.find_first_of(delimiters, lastPos);
}
}
int main() {
string s;
cin >> s;
vector<string> tokens;
int x = 0, y = 0;
split(s, tokens, ";");
for (auto &str : tokens) {
if ((str.size() == 2 && isdigit(str[1])) || (str.size() == 3 && isdigit(str[1]) && isdigit(str[2]))) {
switch (str[0]) {
case 'W':
y += stoi(str.substr(1));
break;
case 'A':
x -= stoi(str.substr(1));
break;
case 'S':
y -= stoi(str.substr(1));
break;
case 'D':
x += stoi(str.substr(1));
break;
default:
break;
}
}
}
cout << x << "," << y << endl;
return 0;
}