题解 | #坐标移动#

坐标移动

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")

全部评论

相关推荐

11-27 12:36
已编辑
门头沟学院 前端工程师
Apries:这个阶段来说,很厉害很厉害了,不过写的简历确实不是很行,优势删掉吧,其他的还行
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务