题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
思路明确,注意代码规范
#include <bits/stdc++.h>
using namespace std;
void zbyd(int& j, int& k, char c, int m) {
if (c == 'A') j -= m;
if (c == 'S') k -= m;
if (c == 'W') k += m;
if (c == 'D') j += m;
}
int main () {
string str;
cin >> str;
vector<char> rec;
vector<int> nums;
for (int i = 0; i < str.size(); i++) {
if ((str[i] == 'A' || str[i] == 'S' || str[i] == 'W' || str[i] == 'D') && (i == 0 || str[i - 1] == ';')) {
int j = i;
while (str[j] != ';') {
j++;
}
//cout << j;
if (j - i <= 3 && j - i >= 2) {
if (j - i == 3) {
if ((str[i + 1] >= '1' && str[i + 1] <= '9') && (str[i + 2] >= '0' && str[i + 2] <= '9')) {
int num;
rec.push_back(str[i]);
num = (str[i + 1] - '0') * 10 + str[i + 2] - '0';
nums.push_back(num);
}
i += 2;
}
if (j - i == 2) {
if (str[i + 1] >= '1' && str[i + 1] <= '9') {
int num;
rec.push_back(str[i]);
num = str[i + 1] - '0';
nums.push_back(num);
}
i += 1;
}
else {
while (++i && i < str.size()) {
if (str[i] == ';')
break;
}
}
}
}
}
int x = 0, y = 0;
for (int i = 0; i < rec.size(); i++) {
zbyd(x, y, rec[i], nums[i]);
}
cout << x << ',' << y << endl;
return 0;
}