题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream>
#include <regex>
using namespace std;
vector<string> split(char chSigned, const string& strInput) {
vector<string> vecTokens;
string strToken;
istringstream tokenStream(strInput);
while (getline(tokenStream, strToken, chSigned)) {
vecTokens.push_back(strToken);
}
return vecTokens;
}
int main() {
string strInput = "";
getline(cin, strInput);
vector<string> vecTmp = split(';', strInput);
int nX = 0, nY = 0;
for (size_t j = 0; j < vecTmp.size(); j++) {
if (vecTmp[j].size() != 0) {
char chDir = vecTmp[j][0];
if (vecTmp[j].size() != 1) {
string strStep = vecTmp[j].substr(1);
int nStep = stoi(strStep);
//if (regex_match(strStep, regex("\\d+")))
if (regex_match(strStep, regex("[0-9]*"))) {
switch (chDir) {
case 'A':
nX -= nStep;
break;
case 'D':
nX += nStep;
break;
case 'W':
nY += nStep;
break;
case 'S':
nY -= nStep;
break;
default:
break;
}
}
}
}
}
cout << nX << "," << nY << endl;
}
// 64 位输出请用 printf("%lld")
腾讯成长空间 1179人发布