题解 | #坐标移动#

坐标移动

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

#include <iostream>
#include <vector>
#include <string>
using namespace std;

vector<int> toCoordinate(string s) {
    int n = s.size();
    if (n < 2 || n > 3) return {0,0};

    char c = s[0];
    if (!(c == 'A' || c == 'D' || c == 'W' || c == 'S')) return {0,0};

    if (n == 2) {
        if (s[1] < '0' || s[1] > '9') return {0,0};
    } else {
        if (s[1] < '1' || s[1] > '9' || s[2] < '0' || s[2] > '9') return {0,0};
    }

    int num=0;
    if(n==2){
        num = s[1]-'0';
    }
    else{
        num = (s[1]-'0')*10+(s[2]-'0');
    }
    if(c=='A') return {-num, 0};
    else if(c=='D') return {num, 0};
    else if(c=='W') return {0, num};
    else return {0,-num};
}
void splitByLabel(string s, char split, vector<string>& string_vecs) {
    int n = s.size();
    for (int i = 0; i < n;) {
        int j = i;
        //while (j < n && s[j] != ';') j++;
        while(s[j]!=split) j++;
        string_vecs.push_back(s.substr(i, j - i));
        i = j + 1;
    }
}

int main() {
    string s;
    getline(cin, s);
    vector<string> vecs;
    splitByLabel(s,';',vecs);
    int x=0,y=0;
    
    for(int i=0;i<vecs.size();i++){
        vector<int>ans = toCoordinate(vecs[i]);
        //cout<<'('<<ans[0]<<','<<ans[1]<<endl;
        x+=ans[0];
        y+=ans[1];
    }
    cout<<x<<','<<y<<endl;


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

全部评论

相关推荐

Java大菜狗:纯纯招黑奴,一天还不到两百那么多要求,还不迟到早退,以为啥啊,给一点工资做一堆活,还以不拖欠员工工资为荣,这是什么值得骄傲的事情吗,纯纯***公司
点赞 评论 收藏
分享
Lorn的意义:1.你这根本就不会写简历呀,了解太少了 2.你这些项目经历感觉真的没啥亮点啊,描述的不行,重写书写一下让人看到核心,就继续海投 注意七八月份ofer还是比较多的,越往后机会越少,抓住时机,抓紧检查疏漏,加油查看图片
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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