题解 | 坐标移动
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream>
#include "vector"
#include "algorithm"
#include "string"
#include "sstream"
using namespace std;
std::vector<std::string> split(std::string str, char delimiter)
{
std::vector<std::string> tokens;
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
int main(){
std::string str;
std::cin >> str;
std::vector<std::string> res = split(str, ';');
int x = 0, y = 0;
for(int i = 0; i < res.size(); i++){
if((res[i][0] == 'A' || res[i][0] == 'S' || res[i][0] == 'D' || res[i][0] == 'W')) {
if (res[i].size() == 2 || res[i].size() == 3) {
int move = 0;
if (res[i].size() == 2) {
if (res[i][1] >= '0' && res[i][1] <= '9') move = res[i][1] - '0';
}
else if (res[i].size() == 3) {
if ((res[i][1] >= '0' && res[i][1] <= '9') && (res[i][2] >= '0' && res[i][2] <= '9'))
move = (res[i][1] - '0') * 10 + (res[i][2] - '0');
}
if (res[i][0] == 'A')x -= move;
else if (res[i][0] == 'D')x += move;
else if (res[i][0] == 'W')y += move;
else if (res[i][0] == 'S')y -= move;
}
}
}
std::cout << x << "," << y;
}
查看19道真题和解析