题解 | #坐标移动#

坐标移动

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

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;

int main(){
    int x = 0, y= 0;
    string in, tmp;
    getline(cin, in);
    vector<string> movements;
    while(in.find(";") != -1)
        in.replace(in.find(";"), 1," ");
    stringstream ss;
    ss << in;
    while(ss>>tmp)
        movements.push_back(tmp);

    //迭代移动动作
    for(auto movement:movements){
        if(movement.size()>3 && movement.size()<2)
            continue;
	  	//这一步有点取巧
        else if(!isupper(movement[0]) || isalpha(movement[2]))
            continue;
        else {
            int m = atoi(movement.substr(1).c_str());
            switch (movement[0]){
                case 'A':x -= m;break;
                case 'D':x += m;break;
                case 'W':y += m;break;
                case 'S':y-= m;break;
                default:
                    continue;
            }
        }
    }
    cout << x<<","<<y<<endl;
    return 0;
}

全部评论

相关推荐

09-03 17:49
已编辑
四川大学 供应链管理
迷茫的大四🐶:提前实习有些坑的,卡毕业后薪资以及能力不达预期提前违约这种
我的OC时间线
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
09-11 10:14
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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