题解 | #坐标移动#

坐标移动

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

秀啊,模拟,尽量少用现成的函数

#include <iostream>
using namespace std;

int main() {
    int x = 0, y = 0;
    string s;
    cin >> s;

    int l = 0, r = 0;
    for ( ; l < s.size() && r < s.size(); ) {
        if (s[r] == ';') { //此时发现子串

            string tmp = s.substr(l, r - l);
            //tmp就是子串,先判断合法性
            if (tmp.size() == 2) {
                if (tmp[0] == 'A') {
                    if (tmp[1] >= '0' && tmp[1] <= '9') {
                        //此时执行a的操作
                        x -= tmp[1] - '0';
                    }
                }

                if (tmp[0] == 'D') {
                    if (tmp[1] >= '0' && tmp[1] <= '9') {
                        //此时执行a的操作
                        x += tmp[1] - '0';
                    }
                }

                if (tmp[0] == 'W') {
                    if (tmp[1] >= '0' && tmp[1] <= '9') {
                        //此时执行a的操作
                        y += tmp[1] - '0';
                    }
                }

                if (tmp[0] == 'S') {
                    if (tmp[1] >= '0' && tmp[1] <= '9') {
                        //此时执行a的操作
                        y -= tmp[1] - '0';
                    }
                }
            }

            if (tmp.size() == 3) {
                if (tmp[1] < '0' || tmp[1] > '9' || tmp[2] < '0' || tmp[2] > '9' ){

                }else{
                    if (tmp[0] == 'A')   x -= stoi(tmp.substr(1,2));
                    if (tmp[0] == 'D')   x += stoi(tmp.substr(1,2));
                    if (tmp[0] == 'W')   y += stoi(tmp.substr(1,2));
                    if (tmp[0] == 'S')   y -= stoi(tmp.substr(1,2));
                }               

            }


            //处理完成以后
            l = r + 1;
            ++r;

        } else {
            ++r;
        }
    }
    cout<<x<<','<<y<<endl;
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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