题解 | #坐标移动#

#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;
}
全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务