题解 | #坐标移动#

坐标移动

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

利用getline()函数进行分隔,得到每一个独立的操作码,然后再对操作码进行判断

/*
HW字符串操作1
*/
#include <bits/stdc++.h>
using namespace std;

int main() {
    string s,t;
    int x=0,y=0;
    while(getline(cin,s)){
        stringstream ss(s);
    while(getline(ss,t,';')){
        if(t.empty()||t.length()>3)continue;
        string _=t.substr(1);
        if(regex_match(_, regex("[0-9]*"))){
            switch(t[0]){
                case 'A':
                    x -=stoi(_);
                    break;
                case 'D':
                    x +=stoi(_);
                    break;
                case 'W':
                    y +=stoi(_);
                    break;
                case 'S':
                    y -=stoi(_);
                    break;
                default:
                    break;
            }
        }
    }
    }
    cout<<x<<","<<y;
    return 0;
}
全部评论

相关推荐

Gaynes:查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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