坐标运动 C++ 正则表达式

坐标移动

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

#include <iostream>
#include <string>
#include <regex>
#include <map>

using namespace std;

int main(int argc, char *argv[])
{
    string str, temp, pattern = "[ADWS]\\d{1,2}$";
    regex r(pattern);

    pair<int, int> p = {0, 0};
    while(getline(cin, str, ';'))
    {
        if(!regex_match(str, r)) continue;
        switch(str[0])
        {
            case 'A':
                {
                    p.first -= stoi(string(str.begin() + 1, str.end()));
                    break;
                }
            case 'D':
                {
                    p.first += stoi(string(str.begin() + 1, str.end()));
                    break;
                }
            case 'W':
                {
                    p.second += stoi(string(str.begin() + 1, str.end()));
                    break;
                }
            case 'S':
                {
                    p.second -= stoi(string(str.begin() + 1, str.end()));
                    break;
                }
        }
    }
    cout << p.first << "," << p.second << endl;
    return 0;
}
全部评论
pattern = "[ADWS]\\d{1,2}$" 为什么不是 "[ADWS]\d{1,2}$" ?
点赞 回复 分享
发布于 2021-04-09 21:34

相关推荐

评论
10
3
分享

创作者周榜

更多
牛客网
牛客企业服务