题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
//代码作者:在发动态的喜羊羊很爱喝奶茶;此代码为转载
#include <bits/stdc++.h>
using namespace std;
class My_vector {
public:
My_vector() {};
My_vector(char s, int num) {
switch (s) {
case 'A':
x = -1;
break;
case 'D':
x = 1;
break;
case 'W':
y = 1;
break;
case 'S':
y = -1;
break;
default:
x = 0, y = 0;
break;
}
if (0 < num < 100) {
x *= num;
y *= num;
} else {
x = 0;
y = 0;
}
}
My_vector operator+(const My_vector&
b) { //类内重载,运算符重载函数作为类的成员函数
My_vector ret;
ret.x = this->x + b.x;
ret.y = this->y + b.y;
return ret;
}
int x = 0, y = 0;
};
void split(string str, vector<string>& res) {
char* str_ch = str.data();
char* p = strtok(str_ch, ";");
while (p != NULL) {
string tem(p);
// cout<<tem<<" ";
res.push_back(tem);
p = strtok(NULL, ";");
}
}
int main() {
string str;
vector<string> res;
My_vector ans;
stringstream ss;
while (getline(cin, str)) {
split(str, res);
for (int i = 0; i < res.size(); i++) {
string tem = res[i];
int tem_int;
if (tem.size() == 1 || tem.size() > 3 || tem[1] > 61 || tem[2] > 61)
continue;
tem_int = std::stoi(tem.substr(1));
//cout <<tem_int<<" ";
My_vector temp(tem[0], tem_int);
ans = ans + temp;
}
}
cout << ans.x << "," << ans.y;
}
查看14道真题和解析