题解 | #HJ17 坐标移动#

坐标移动

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

#include <bits/stdc++.h>
using namespace std;
unordered_set<char> st;
int check(string str) {
    if (str.size() > 3 || str.size() == 0) return -1;
    if (st.count(str[0]) == 0) return -1;
    if (str.size() == 2) {
        if (!isdigit(str[1])) return -1;
        return str[1] - '0';
    }
    if (str.size() == 3) {
        if (isdigit(str[1]) && isdigit(str[2])) {
            return stoi(str.substr(1, 2));
        } else return -1;
    }
    return -1;
}
int main() {
    int x = 0, y = 0;
    st.insert('A');
    st.insert('W');
    st.insert('S');
    st.insert('D');
    string s;
    int index = 0;
    getline(cin, s);
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == ';') {
            string temp = s.substr(index, i - index);
            int res = check(temp);
            if (res != -1) {
                if (temp[0] == 'A') x -= res;
                if (temp[0] == 'D') x += res;
                if (temp[0] == 'S') y -= res;
                if (temp[0] == 'W') y += res;
            }
            index = i + 1;
        }
    }
    cout << x << "," << y;
    return 0;
}

全部评论

相关推荐

07-15 18:09
门头沟学院 Java
点赞 评论 收藏
分享
06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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