题解 | #坐标移动#

坐标移动

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

#include <bits/stdc++.h>
#include <iostream>
#include <string>

using namespace std;

unordered_map<char, pair<int, int>> m = {
    {'A', make_pair(-1, 0)},
    {'W', make_pair(0, 1)},
    {'S', make_pair(0, -1)},
    {'D', make_pair(1, 0)}
};

void process(string str, pair<int, int>& res/*int x, int y*/){
    int divnum = 0;
    for(char c : str){
        if(c == ';') divnum++;
    }
    
    string s = "";
    stringstream iss(str);
    while(divnum--){
        getline(iss, s, ';');
        //cout<<s<<endl; 
        if(s[0] == 'A' || s[0] == 'W' || s[0] == 'S' || s[0] == 'D'){
            string tmp = s.substr(1);
            int num = 0;
            for(char c : tmp){
                if(c >= 'A' && c <= 'Z'){
                    num = 0;
                    break;
                }
                else{
                    num = num * 10 + (c - '0');
                }
            }        
            //cout << num << endl;
            
            res.first += m[s[0]].first * num;
            res.second += m[s[0]].second * num;
        }  
        
    }
    
    return;
}

int main(){
    string str = "";
    getline(cin, str);
    
    //int x = 0, y = 0;
    pair<int, int> res({0, 0});
    process(str, res);
    
    cout << res.first << "," << res.second << endl;
    
    return 0;
}
华为题库题解 文章被收录于专栏

牛客华为题库的题解

全部评论

相关推荐

大鹏随风起:不用打开评论区我就知道会有什么评论
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务