题解 | 坐标移动
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
// 简单模拟。人和代码一样帅🙂!
#include <bits/stdc++.h>
#include <utility>
using namespace std;
pair<int, int> p = {0, 0};
void move(char c, int step){
switch (c) {
case 'A':
p.first = p.first - step;
break;
case 'D':
p.first = p.first + step;
break;
case 'S':
p.second = p.second - step;
break;
case 'W':
p.second = p.second + step;
break;
default:
break;
}
}
void handle(string s){
if(s.empty() || (s[0] != 'A' && s[0] != 'D' && s[0] != 'S' && s[0] != 'W')) return;
int step = 0;
for(int i = 1; i < s.size(); i ++ ){
if(s[i] > '9' || s[i] < '0') return;
step = step * 10 + s[i] - '0';
}
move(s[0], step);
}
int main() {
string s;
cin >> s;
string action;
for(const auto &c : s){
if(c == ';'){
// 处理
handle(action);
// cout << "action: " << action << "\n";
action.clear();
}else{
action.push_back(c);
}
}
cout << p.first << "," << p.second << "\n";
return 0;
}
// 64 位输出请用 printf("%lld")
查看17道真题和解析