题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream>
#include<array>
#include<algorithm>
#include <sstream>
using namespace std;
int main() {
std::string MoveQuery;
array<int, 2> arr = {0};
std::cin >> MoveQuery;
std::stringstream ostr(MoveQuery);
std::string MoveDistance;
while (getline(ostr,MoveQuery,';')) {//用字符串流将每次移动切割出来
if(MoveQuery.empty()){
continue;
}
MoveDistance.clear();
MoveDistance.assign(MoveQuery, 1);//将字母AWSD后的数字给提取出来
if (MoveDistance.find_first_not_of("0123456789") != MoveDistance.npos) {
continue;
}
if (MoveQuery[0] == 'A' || MoveQuery[0] == 'a') {
arr[0] -= std::stoi(MoveDistance);
}
if (MoveQuery[0] == 'S' || MoveQuery[0] == 's') {
arr[1] -= std::stoi(MoveDistance);
}
if (MoveQuery[0] == 'W' || MoveQuery[0] == 'w') {
arr[1] += std::stoi(MoveDistance);
}
if (MoveQuery[0] == 'D' || MoveQuery[0] == 'd') {
arr[0] += std::stoi(MoveDistance);
}
}
std::cout << arr[0] << "," << arr[1] << std::endl;
}
// 64 位输出请用 printf("%lld")

查看1道真题和解析
