题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// 移动坐标轴
int main() {
string str;
while (getline(cin, str)) { // 注意 while 处理多个 case
vector<string> vec;
string temp;
// 把所有坐标放进一个数组中
for (int i = 0; i < str.size(); i++) {
if (str[i] == ';') {
vec.push_back(temp);
temp.clear();
continue;
} else {
temp += str[i];
}
}
int xy[2] = {0, 0};
for (int i = 0; i < vec.size(); i++) {
// 判定坐标合法性
if (vec[i].size() > 3 || vec[i].size() <= 1 || vec[i][1] == 'A' ||
vec[i][1] == 'D' || vec[i][1] == 'W' || vec[i][1] == 'S' ||
vec[i][2] == 'A' || vec[i][2] == 'D' || vec[i][2] == 'W' ||
vec[i][2] == 'S') {
continue;
}
string tstr = vec[i];
// 坐标加减
int num = 0;
if (tstr[0] == 'A') {
num = stoi(tstr.substr(1, 2));
xy[0] -= num;
tstr.clear();
} else if (tstr[0] == 'D') {
num = stoi(tstr.substr(1, 2));
xy[0] += num;
tstr.clear();
} else if (tstr[0] == 'S') {
num = stoi(tstr.substr(1, 2));
xy[1] -= num;
tstr.clear();
} else if (tstr[0] == 'W') {
num = stoi(tstr.substr(1, 2));
xy[1] += num;
tstr.clear();
}
}
cout << xy[0] << "," << xy[1] << endl;
}
}
// 64 位输出请用 printf("%lld")
小天才公司福利 1171人发布