题解 | #坐标移动#

坐标移动

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

#include <iostream>
using namespace std;
#include <map>

bool isNum(string s){
    int len=s.size();
    if(len==0||len>2){
        return false; //判空或者大于两位数
    }
    for(int i=0;i<len;i++){
        if(s[i]>='0'&&s[i]<='9'){
            continue;
        }
        else{
            return false;
        }
    }
    return true;
}
int main() {
    string str;
    pair<int,int>p(0,0);
    while(getline(cin,str,';')){
        if(str.empty()){
            continue;
        }
        string s=str.substr(1);
        if(isNum(s)){
            switch (str[0]){
                case 'A':
                    p.first-=stoi(s);
                    break;
                case 'D':
                    p.first+=stoi(s);
                    break;
                case 'W':
                    p.second+=stoi(s);
                    break;
                case 'S':
                    p.second-=stoi(s);
                    break;
                default:
                    break;
            }
        }
    }
    cout<<p.first<<","<<p.second;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务