题解 | #坐标移动#

坐标移动

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

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

bool isnum(const string& s)
{
    if (s == "") {
        return false;
    }
    int flag = 0;
    for (const auto & sub: s) {
        if(sub <'0' || sub>'9')
        {
            return false;
        }
    }
    return true;
}

int main() {
    int x=0,y=0;
    string input,temp,num;
    char split = ';';
    getline(cin,input);
    istringstream iss(input);
    while (getline(iss,temp,split)) {
        if (temp == "") {
            continue;
        }
        num = temp.substr(1);
        if(isnum(num))
        {
            switch (temp[0]) {
                case 'A':
                x -=  stoi(num) ;
                break;
                case 'D':
                x +=  stoi(num) ;
                break;
                case 'W':
                y +=  stoi(num) ;
                break;
                case 'S':
                y -=  stoi(num) ;
                break;
                default:
                break;
            }

        }
    }
    cout<<x<<','<<y<<endl;

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

老板加个卤鸡蛋:HR看了以为来卧底来了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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