题解 | #坐标移动#__huawei-no.17

坐标移动

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

#include <iostream>
#include <string>
using namespace std;
bool isNum(string s) {
    int len = s.size();
    for (int i = 0 ; i < len; i++) {
        if ('0' <= s[i] && s[i] <= '9') continue;
        else return false;
    }
    return true;
}

int main() {
    string s;
    pair<int, int>p(0, 0);
    while (getline(cin, s, ';')) {
        if (s.empty())
            continue;
        string s1 = s.substr(1);        //提取第一个字符之后的
        if (isNum(s1)) {
            switch (s[0]) {
                case 'A':
                    p.first -= stoi(s1);
                    break;
                case 'D':
                    p.first += stoi(s1);
                    break;
                case 'W':
                    p.second += stoi(s1);
                    break;
                case 'S':
                    p.second -= stoi(s1);
                    break;
                default:
                    break;
            }
        }
    }
    cout<<p.first<<","<<p.second;
    return 0;

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

1、首先,利用getline可以指定分隔符进行读取

2、如何判断数字有效,共同点都在第一个字母之后的数字,所以要substr

3、使用了一个哈希表的东西?pair我不是很熟悉他的使用方法,后面一查才知道这是模板类,还有模板函数等等

全部评论

相关推荐

神哥了不得:你简历字体有点不太协调呀,下面的字实在太小了呀,而且项目也不太行,建议换几个高质量的项目,面试会多很多
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务