题解 | #坐标移动#

坐标移动

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 17:10
什么素质,我请问呢,要掉小珍珠了。。。又憋屈又生气
苍蓝星上艾露:给它们能的,一群dinner牛马挥刀向更弱者罢了。我写的开源求职AI co-pilot工具,优化你的简历,找到你匹配的岗位,定制你的简历,并让你做好面试准备https://github.com/weicanie/prisma-ai
点赞 评论 收藏
分享
07-10 14:08
已编辑
江西农业大学 Java
念旧select:做完把项目放到自己硬盘里给他看,看完拷走
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 11:31
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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