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

#刷题记录#
全部评论

相关推荐

04-09 09:47
门头沟学院 Java
Arbelite_:2-3k,这工资还不如去摇奶茶
点赞 评论 收藏
分享
谁知道呢_:你好,我是炮灰n+1号
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务