题解 | #坐标移动# C++

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

#include <iostream>
#include <vector>
#include <cctype>
using namespace std;
void SplitString(const std::string& s, std::vector<std::string>& v,
                 const std::string& c) {
    std::string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while (std::string::npos != pos2) {
        v.push_back(s.substr(pos1, pos2 - pos1));
        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if (pos1 != s.length())
        v.push_back(s.substr(pos1));
}

int main() {
    string strRaw;
    cin >> strRaw;
    vector<string> strs;
    SplitString(strRaw, strs, ";");
    pair<int, int> point = {0, 0};
    for (auto str : strs) {
        if (!isalpha(str[0]) || !isdigit(str[1]) || (str.size() > 2 && !isdigit(str[2]))) {
            continue;
        }
        int len = stoi(str.substr(1, 2));
        if (str[0] == 'A') point.first -= len;
        if (str[0] == 'D') point.first += len;
        if (str[0] == 'W') point.second += len;
        if (str[0] == 'S') point.second -= len;
    }
    cout << to_string(point.first) + "," + to_string(point.second) << endl;
    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务