题解 | 坐标移动

坐标移动

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

#include <cctype>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cctype>

int main(){
    std::string s, token;
    std::vector<int> cor={0,0};
    std::map<char, int> com;
    char ori;
    while(std::getline(std::cin, token, ';')){
        if(token.size() < 2) continue;
        if(std::isalpha(token[0])){
            ori = token[0];
            bool digit = true;
            for(auto c: token.substr(1)){
                if(!std::isdigit(c)) digit = false;
            }
            if(digit){
                int dis = std::stoi(token.substr(1));
                if(com[ori]){
                    com[ori] += dis;
                }else com[ori] = dis;
            }
        }
    }
    for(auto [ori, dis]: com){
        switch(ori){
            case 'A':
            cor[0] -= dis;
            break;
            case 'D':
            cor[0] += dis;
            break;
            case 'W':
            cor[1] += dis;
            break;
            case 'S':
            cor[1] -= dis;
            break;
        }
    }
    std::cout << cor[0] << ',' << cor[1];
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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