题解 | #坐标移动#

坐标移动

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

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

vector<int> calculateCoord(string str)
{
    vector<int> v;
    int x = 0, y = 0, feet = 0;

    for (int i = 0; i < str.length(); i++){
        if (str[i] == 'A' || str[i] == 'D' || str[i] == 'W' || str[i] == 'S'){
           ////////////////////////////
            // cout << " 1111111: "<< str[i] << ", i : " <<i 
            // << ",  str.length:" <<str.length() << endl;

            if (i > 1 && str[i-1] != ';') continue;

            string s = str.substr(i+1, str.length() - i);

            int p = s.find(';');
            feet = 0;
            if (p > 0){
                string tmp = s.substr(0, p);
                for (int j = 0; j < tmp.length(); j++){
                    if (tmp[j] >= '0' && tmp[j] <= '9'){
                        feet = feet*10 + tmp[j] - '0';
                    }
                    else{
                        feet = -1; break;
                    }
                }
            }
           ////////////////////////////
            // cout << str[i] << ": " << feet << endl;

             if (feet > 0){
                    if (str[i] == 'A'){ 
                        x -= feet;
                    }
                    else if (str[i] == 'D'){
                        x += feet;
                    }
                    else if (str[i] == 'W'){
                        y += feet;
                    }
                    else if (str[i] == 'S'){
                        y -= feet;
                    }
                }
        }
    }

    v.push_back(x);
    v.push_back(y);
    return v;
}

int main() {
    string str;
    getline(cin, str);

    vector<int> v = calculateCoord(str);
    if (v.size() > 1){
        cout << v[0] << "," << v[1] <<endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

争当牛马还争不上
码农索隆:1.把简历改哈 2.猛投,狠投 3.把基础打牢 这样你在有机会的时候,才能抓住
点赞 评论 收藏
分享
迟缓的斜杠青年巴比Q...:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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