题解 | #坐标移动#

坐标移动

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

#include <cctype>
#include <iostream>
#include <istream>
#include <string>
#include <sstream>
using namespace std;

bool isNum(const string &len) {
    for (auto item : len) {
        if (!isdigit(item)) return false;
    }
    return true;
}

void dealA(int &x, const string &len) {
    if (!isNum(len)) return;
    int move = stoi(len, 0);
    x -= move;
}

void dealD(int &x, const string &len) {
    if (!isNum(len)) return;
    int move = stoi(len, 0);
    x += move;
}

void dealW(int &y, const string &len) {
    if (!isNum(len)) return;
    int move = stoi(len, 0);
    y += move;
}

void dealS(int &y, const string &len) {
    if (!isNum(len)) return;
    int move = stoi(len, 0);
    y -= move;
}

void dealPos(int &x, int &y, string &pos) {
    if (pos.length() > 3 || pos.length() < 2) return;

    switch (pos[0]) {
        case 'A': dealA(x, string(pos.begin() + 1, pos.end())); break;
        case 'D': dealD(x, string(pos.begin() + 1, pos.end())); break;
        case 'W': dealW(y, string(pos.begin() + 1, pos.end())); break;
        case 'S': dealS(y, string(pos.begin() + 1, pos.end())); break;
    }
}

int main() {
    
    int x = 0, y = 0;
    string posStr, pos;

    cin >> posStr;
    istringstream iss(posStr);

    while (getline(iss, pos, ';')) {
        dealPos(x, y, pos);
    }
    cout << x << ',' << y << endl;


}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

北枳的南橘:建议大家务必去和他沟通一下,像极了小学生对你侃侃而谈国际局势😂
找AI工作可以去哪些公司...
点赞 评论 收藏
分享
牛客48784610...:深圳的变成录用进行中,这个是稳了吗,还没有收到邮件
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务