题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream>
#include <vector>
using namespace std;
bool check(string& s) {
if (s[0] == 'A' || s[0] == 'S' || s[0] == 'D' || s[0] == 'W') {
for (int i = 1; i < s.size(); ++i) {
if (s[i] < '0' || s[i] > '9') {
return false;
}
}
return true;
}
return false;
}
int main() {
vector<string> str;
string s;
cin >> s;
int start = 0;
for(int i = 0; i < s.size(); ++i){
if(s[i] == ';'){
str.push_back(s.substr(start, i - start));
start = i + 1;
}
}
pair<int, int> res = {0, 0};
for(int i = 0; i < str.size(); ++i) {
if (check(str[i])) {
string c = str[i].substr(1, str[i].size() - 1);
if (str[i][0] == 'A') {
res.first -= stoi(c);
}
if (str[i][0] == 'D') {
res.first += stoi(c);
}
if (str[i][0] == 'S') {
res.second -= stoi(c);
}
if (str[i][0] == 'W') {
res.second += stoi(c);
}
}
}
cout << res.first << ',' << res.second;
return 0;
}
// 64 位输出请用 printf("%lld")
