题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream>
#include <vector>
using namespace std;
vector<int> calculateCoord(string str)
{
vector<int> v;
int x = 0, y = 0, feet = 0;
for (int i = 0; i < str.length(); i++){
if (str[i] == 'A' || str[i] == 'D' || str[i] == 'W' || str[i] == 'S'){
////////////////////////////
// cout << " 1111111: "<< str[i] << ", i : " <<i
// << ", str.length:" <<str.length() << endl;
if (i > 1 && str[i-1] != ';') continue;
string s = str.substr(i+1, str.length() - i);
int p = s.find(';');
feet = 0;
if (p > 0){
string tmp = s.substr(0, p);
for (int j = 0; j < tmp.length(); j++){
if (tmp[j] >= '0' && tmp[j] <= '9'){
feet = feet*10 + tmp[j] - '0';
}
else{
feet = -1; break;
}
}
}
////////////////////////////
// cout << str[i] << ": " << feet << endl;
if (feet > 0){
if (str[i] == 'A'){
x -= feet;
}
else if (str[i] == 'D'){
x += feet;
}
else if (str[i] == 'W'){
y += feet;
}
else if (str[i] == 'S'){
y -= feet;
}
}
}
}
v.push_back(x);
v.push_back(y);
return v;
}
int main() {
string str;
getline(cin, str);
vector<int> v = calculateCoord(str);
if (v.size() > 1){
cout << v[0] << "," << v[1] <<endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")