题解 | #坐标移动#

坐标移动

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

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

bool isNum(string& str) {
    for (auto s : str) {
        if (!isdigit(s)) return false;
    }
    return true;
}


int main() {
    pair<int,int> pos(0,0);
    string str;
    while (getline(cin, str, ';')) {
        if(str.empty()) continue;
        string nstr = str.substr(1);
        if(isNum(nstr) && nstr.length() <=2) {
            switch(str[0]) {
                case 'A': 
                    pos.first -= stoi(nstr);
                    break;
                case 'D':
                    pos.first += stoi(nstr);
                    break;
                case 'W':
                    pos.second += stoi(nstr);
                    break;
                case 'S':
                    pos.second -= stoi(nstr);
                    break;
                default:
                    break;
            }
        }
    }
    cout << pos.first << "," << pos.second << endl;
}
// 64 位输出请用 printf("%lld")




全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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