题解 | #坐标移动#

坐标移动

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

#include <iostream>
#include <vector>
using namespace std;
bool check(string& s) {
    if (s[0] == 'A' || s[0] == 'S' || s[0] == 'D' || s[0] == 'W') {
        for (int i = 1; i < s.size(); ++i) {
            if (s[i] < '0' || s[i] > '9') {
                return false;
            }
        }
        return true;
    }
    return false;
}
int main() {
    vector<string> str;
    string s;
    cin >> s;
    int start = 0;
    for(int i = 0; i < s.size(); ++i){
        if(s[i] == ';'){
            str.push_back(s.substr(start, i - start));
            start = i + 1;
        }
    }
    pair<int, int> res = {0, 0};
    for(int i = 0; i < str.size(); ++i) {
        if (check(str[i])) {
            string c = str[i].substr(1, str[i].size() - 1);
            if (str[i][0] == 'A') {
                res.first -= stoi(c);
            }
            if (str[i][0] == 'D') {
                res.first += stoi(c);
            }
            if (str[i][0] == 'S') {
                res.second -= stoi(c);
            }
            if (str[i][0] == 'W') {
                res.second += stoi(c);
            }
        }
    }

    cout << res.first << ',' << res.second;

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

全部评论

相关推荐

菠落蜜:这个是系统自动投的,不是hr主动打招呼。更抽象的还有ai回复
我的秋招日记
点赞 评论 收藏
分享
Sigma429:极兔啊,薪资开的巨低,还在上海,索性不做笔试了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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