题解 | #坐标移动#

坐标移动

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

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

bool isValid(string const& str) {  // 判断分割的字符串是否合法
    if (str.size() <= 1) return false;
    if ( !isalpha(str[0]) ) return false;
    for (int i = 1; i < str.size(); i++) {
        if (!isdigit(str[i])  ) return false;
    }
    return true;
}
void move(string& str, int& x, int& y) {  //根据合法字符串对左边进行移动
    int num = stoi(str.substr(1));
    switch (str[0]) {
        case 'W':
            y = y + num;
            break;
        case 'A':
            x = x - num;
            break;
        case 'S':
            y = y - num;
            break;
        case 'D':
            x = x + num;
            break;
    }
}
/*
int main() {
    int x = 0, y = 0;
    string strs;
    getline(cin, strs, '\n');
    string str = "";
    for (auto s : strs) {
        if (s == ';') {
            if (isValid(str)) {
                move(str, x, y);
                // cout<<x<<","<<y<<endl;
                str = "";
            } else {
                str = "";
            }

        } else {
            str += s;
        }
    }
    cout << x << "," << y << endl;
    return 0 ;
}*/

int main() {
    int x = 0, y = 0;
    string strs;
    getline(cin, strs);  // 获取原始字符串
    vector<string> str_vec;
    string str;
    stringstream ss;
    ss<< strs;
    while(getline(ss, str, ';')){  //根据 ';'对原始命令进行分割
        if(isValid(str)){   //判断分割得到的命令是否有效
            str_vec.push_back(str);
        }
    }
    for(auto s : str_vec){  //根据有效命令 对坐标进行移动
        move(s, x, y);
    }
    cout << x << "," << y << endl;
    return 0 ;
}

// 64 位输出请用 printf("%lld")

全部评论

相关推荐

已oc&nbsp;云智断更了好几天,也有一些话想说,继续更新一篇云智timeline&nbsp;4.18&nbsp;一面&nbsp;半个小时后约二面&nbsp;4.21二面&nbsp;当晚&nbsp;约hr面&nbsp;4.23hr面&nbsp;4.30&nbsp;发offer之前美团的二面挂了,进入人才库,后面又被捞起来面试,4.30号&nbsp;美团又一面,现在还没出一面结果感觉也不报什么希望,就算一面过了,还有二面,我经不起深入拷打,唉,真的,好难五一躺平了五天,吃吃玩玩睡睡~还要担心毕业,科研更是难,唉暑期可能就到此为止了,后面没有时间在这个上面了,要抓紧时间做科研,为了后面能出去实习。大厂,秋招再见!!!有一些感慨:4.1是我的第一次面试,美团,面试的时候紧张到浑身发...
daisy9542:我今晚也是美团一面,已经第六次了。我也面了其他的,没拿到 offer。但我想开了,要按照自己的节奏来,找暑期转正然后秋招大杀四方并不是唯一的出路,其实还有很多选择的,有 0 实习最后秋招拿 offer 了,也有不选择互联网去国企的外企的,考编的,创业的。现在的失败不代表以后的路都是黑暗的,只不过可能运气还没降临到头上。所以现在要做的,就是放平心态,提升自己,通过面试了解到自己的优点和不足,争取下次机会来了能好好抓住
点赞 评论 收藏
分享
劝退式:感觉有人回才是不正常的
点赞 评论 收藏
分享
逆流河上万仙退:我觉得佬没必要 学历在这里 去了也不会对履历有很大提升 只是有可能让自己更熟练 是我的话会更倾向于找暑期或者中大厂日常
查看13道真题和解析
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务