题解 | #坐标移动#

坐标移动

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

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int x=0,y=0;
    string str;
    while (cin >> str) {
        vector<string> steps;
        //拆分字符串
        int wordlen = 0;
        for (int i = 0; i < str.length(); i++) {
            while (str[i] != ';') {
                wordlen++;
                i++;
            }
            steps.push_back(str.substr(i - wordlen, wordlen));
            wordlen = 0;
        }
        for (int i = 0; i < steps.size(); i++) {
                int num = 0;
                if (steps[i].size() == 3) {
                    if (steps[i][1] >= '0' && steps[i][1] <= '9' && steps[i][2] >= '0' &&
                            steps[i][2] <= '9')
                        num = 10 * int(steps[i][1] - '0') + int(steps[i][2] - '0');
                    else continue;
                } 
                else if (steps[i].size() == 2) {
                    if (steps[i][1] >= '0' && steps[i][1] <= '9')
                        num = int(steps[i][1] - '0');
                    else continue;
                }
                else continue;
            switch (steps[i][0]) {
                case 'A':
                    x -= num;
                    break;
                case 'D':
                    x += num;
                    break;
                case 'S':
                    y -= num;
                    break;
                case 'W':
                    y += num;
                    break;
                default:
                    break;
            
        }
    }
    

}
    cout<<x<<","<<y<<endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务