题解 | #坐标移动#

坐标移动

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

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

bool isDir(char c) {
    return c == 'A' || c == 'D' || c == 'W' || c == 'S';
}

bool isNum(char c) {
    return c >= '0' && c <= '9';
}


int main() {
    char c;
    int x = 0;
    int y = 0;
    int l = 0;
    vector<char> s;
    while (cin >> c) { // 注意 while 处理多个 case
        if (c == ';') {
            if (l == 3) {
                if (isDir(s[0]) && isNum(s[1]) && isNum(s[2])) {
                    int dis = (s[1] - '0') * 10 + (s[2] - '0');
                    if (s[0] == 'A') {
                        x -= dis;
                    } else if (s[0] == 'D') {
                        x += dis;
                    } else if (s[0] == 'W') {
                        y += dis;
                    } else if (s[0] == 'S') {
                        y -= dis;
                    }
                }
            } else if (l == 2) {
                if (isDir(s[0]) && isNum(s[1])) {
                    int dis = (s[1] - '0');
                    if (s[0] == 'A') {
                        x -= dis;
                    } else if (s[0] == 'D') {
                        x += dis;
                    } else if (s[0] == 'W') {
                        y += dis;
                    } else if (s[0] == 'S') {
                        y -= dis;
                    }
                }
            }
            s.clear();
            l = 0;
        } else {
            s.push_back(c);
            l++;
        }
    }
    cout << x << ',' << y << endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

07-09 18:33
门头沟学院 Java
这么逆天每年都有人去???&nbsp;填多益网申就是大型的服从性测试
鲁大牛:辅导员在群里发了这个公司我就申了一下。网申居然要写当场开摄像头写两篇不少于三百字的作文。太逆天了
点赞 评论 收藏
分享
仁者伍敌:难怪小公司那么挑剔,让你们这些大佬把位置拿了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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