题解 | #坐标移动#

坐标移动

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

不用正则表达式

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
void dearCoordinate(int& x, int& y, string mv){
    if(mv.size()==0) return;
    int cnt(0);
    for(auto item:mv){
        if((item>'9'||item < '0') && item!='A'&&item!='S'&&item!='W'&&item!='D') return;
        if(item=='A'||item=='S'||item=='W'||item=='D') {
            if(++cnt>=2) return;
        }
    }
    char mvS = mv[0];
    stringstream ss(mv.substr(1, mv.length()-1));
    int step;
    ss>>step;
    switch(mvS){
        case 'A':
            x-=step;
            break;
        case 'D':
            x+=step;
            break;
        case 'W':
            y+=step;
            break;
        case 'S':
            y-=step;
            break;
    }
}

int main(){
    string str;
    cin>>str;
    vector<string> steps;

    for(auto& item:str){
        if(item==';'){
            item = ' ';
        }
    }

    stringstream ss(str);
    int x(0),y(0);
    while(ss){
        string temp;
        ss>>temp;
        dearCoordinate(x, y, temp);
    }
    cout<<x<<','<<y<<endl;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 13:15
点赞 评论 收藏
分享
Lorn的意义:你这种岗位在中国现在要么牛马天天加班,要么关系户进去好吃好喝,8年时间,真的天翻地覆了,对于资本来说你就说一头体力更好的牛马,哎,退伍没有包分配你真的亏了。
点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
程序员饺子:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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