题解 | #坐标移动#

坐标移动

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

#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;

static int x = 0,y = 0;
void WASD(string& s)
{
    if((s.size() == 3 || s.size() == 2) && isupper(s[0]))
    {
        int offset;
        if(s.size() == 3 && isdigit(s[1]) && isdigit(s[2]))  offset = atoi(s.substr(1,2).c_str());
        else if(s.size() == 2 && isdigit(s[1]))  offset = atoi(&s[1]);
        else  return;;
        switch(s[0])
        {
            case 'W':
                y += offset;
                break;
            case 'A':
                x -= offset;
                break;
            case 'S':
                y -= offset;
                break;;
            case 'D':
                x += offset;
                break;
            default:
                break;
        }
    }
}

int main() {
    string s;
    while (getline(cin,s)) { // 注意 while 处理多个 case
        stringstream ss(s);
        string st;
        while(getline(ss,st,';'))
        {
            if(!st.empty())
            {
                WASD(st);
            }
        }
        cout << x << "," << y << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

海螺很能干:每次看到这种简历都没工作我就觉得离谱
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务