题解 | 坐标移动

#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
    string a;
    cin >> a;
    vector<string> strVec;
    while (!a.empty()) {
        int idx = a.find(';');
        string t = a.substr(0, idx);
        strVec.push_back(t);
        a.erase(0, idx+1);
    }

    pair<int, int> coordinate(0, 0);
    for (auto cell: strVec) {
        if (cell.length() < 2) {
            continue;
        }
        char ch = cell.front();
        cell.erase(0, 1);
        int k = stoi(cell);
        if (cell != to_string(k)) {
            continue;
        }
        switch (ch) {
            case 'A':
                coordinate.first -= k;
                break;
            case 'D':
                coordinate.first += k;
                break;
            case 'W':
                coordinate.second += k;
                break;
            case 'S':
                coordinate.second -= k;
                break;
            default:
                continue;
        }
    }
    cout << coordinate.first << ',' << coordinate.second;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务