题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <cctype>
#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
string s;
getline(cin, s);
vector<string> v;
vector<int> vi;
vector<char> vc;
for (int i = 0; i < s.size(); i = i + 1) {
string temp;
while (s[i] != ';') {
temp.push_back(s[i]);
i = i + 1;
}
v.push_back(temp);
}
for (int i = 0; i < v.size(); i++) {
if (v[i].size() == 2 && isalpha(v[i][0]) && isdigit(v[i][1])) {
vc.push_back(v[i][0]);
vi.push_back(v[i][1]-'0');
} else if (v[i].size() == 3 && isalpha(v[i][0]) && isdigit(v[i][1]) &&
isdigit(v[i][2])) {
vc.push_back(v[i][0]);
vi.push_back((v[i][1] - '0')*10 + (v[i][2] - '0'));
}
}
int x = 0,y=0;
for (int j = 0; j < vi.size(); j++) {
if (vc[j] == 'W')
y += vi[j];
if (vc[j] == 'A')
x -= vi[j];
if (vc[j] == 'S')
y -= vi[j];
if (vc[j] == 'D')
x += vi[j];
}
cout << x << ',' << y << endl;
}
// 64 位输出请用 printf("%lld")
查看1道真题和解析
腾讯云智研发成长空间 5088人发布