题解 | #坐标移动#

坐标移动

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")

全部评论

相关推荐

点赞 评论 收藏
分享
你背过凌晨4点的八股文么:简历挂了的话会是流程终止,像我一样
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务