HJ17题解 | #坐标移动#

坐标移动

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

#include <iostream>
using namespace std;

int main() {
    string input;
    int x = 0, y = 0;
    getline(cin, input);
    // input = "A10;S20;W10;D30;X;A1A;B10A11;;A10;";
    string tmp;
    size_t pos = input.find(";");
    while (pos != string::npos) {
        tmp = input.substr(0, pos);
        input = input.substr(pos + 1);
        if (tmp.empty()) {
            pos = input.find(";");
            continue;
        }
        int size = tmp.size();
        if (size != 2 && size != 3) {
            pos = input.find(";");
            continue;
        }
        // is = false;
        string numStr = tmp.substr(1);
        bool isLeagle = true;
        int num = 0;
        for (auto item : numStr) {
            if (item < '0' || item > '9') {
                isLeagle = false;
            }
        }
        if (!isLeagle) {
            pos = input.find(";");
            continue;
        }
        if (tmp[0] == 'A') {
            x += atoi(numStr.c_str()) * (-1);
        } else if (tmp[0] == 'D') {
            x += atoi(numStr.c_str()) * (1);
        } else if (tmp[0] == 'W') {
            y += atoi(numStr.c_str()) * (1);
        } else if (tmp[0] == 'S') {
            y += atoi(numStr.c_str()) * (-1);
        } else {

        }
        pos = input.find(";");
    }
    cout << x << "," << y;

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

#刷题记录#
全部评论

相关推荐

不愿透露姓名的神秘牛友
02-14 11:10
点赞 评论 收藏
分享
野猪不是猪🐗:这种直接口头上答应,骗面试,面完了直接拉黑,相当于给自己攒面经了(
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务