题解 | #坐标移动#

坐标移动

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

#include <cctype>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool isdigit(std::string str){
    for(char c : str){
        if(c < 48 || c > 57)
            return false;
    }return true;
}
int main() {
    string str;
    cin >> str;
    istringstream iss(str);
    string move;
    int x = 0,y = 0;

    while(getline(iss,move,';')){
        // cout << move << endl;
        if(move.length() == 2 || move.length() == 3){
            
            if(move[0] == 'A' || move[0] == 'D' || move[0] == 'W' || move[0] == 'S'){
                string dis = move.substr(1);
                
                if(isdigit(dis)){
                    
                    int num = stoi(dis);
                    // cout << move << " " << num<< endl;
                    switch (move[0]) {
                    case 'A':x -= num;break;
                    case 'D':x += num;break;
                    case 'W':y += num;break;
                    case 'S':y -= num;break;
                    }
                }
                   
            }
        }
    }
    cout << x << "," << y << endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

03-31 16:42
已编辑
郑州西亚斯学院 后端
Java抽象带篮子:你简历少了几个模块看上去就感觉信息很少,简历怎么写可以看看我发的帖子
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务