题解 | #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-17 11:50
门头沟学院 Java
投递腾讯等公司7个岗位
点赞 评论 收藏
分享
流浪的神仙:无恶意,算法一般好像都得9硕才能干算法太卷啦
点赞 评论 收藏
分享
Java大菜狗:纯纯招黑奴,一天还不到两百那么多要求,还不迟到早退,以为啥啊,给一点工资做一堆活,还以不拖欠员工工资为荣,这是什么值得骄傲的事情吗,纯纯***公司
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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