题解 | #坐标移动#c++

坐标移动

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

#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool isValid(string s){
    if(s.size() == 0 || s.size() == 1 || s.size() >=4){
        return false;
    }
    
    if(s[0]!='A' && s[0]!='D' && s[0]!='W' && s[0]!='S'){
        return false;
    }
    
    if(s[1]<'1' || s[1] > '9'){
        return false;
    }
    
    if(s.size()==2){
        return true;
    }
    
    if(s[2]<'0' || s[2] > '9'){
        return false;
    }
    
    return true;
}

int main(void){
    char ch;
    string command;
    vector<int> coor(2,0);
    
    while((ch=getchar()) !='\n'){
        if(ch!=';'){
            command.push_back(ch);
            continue;
        }
        
        if(isValid(command)){
            if(command[0]=='A'){
                coor[0] -= stoi(command.substr(1));
            }
             if(command[0]=='D'){
                coor[0] += stoi(command.substr(1));
            }
             if(command[0]=='W'){
                coor[1] += stoi(command.substr(1));
            }
             if(command[0]=='S'){
                coor[1] -= stoi(command.substr(1));
            }
        }
        command.clear();
        
    }
    
    cout<<coor[0]<<','<<coor[1]<<endl;
    
    return 0;
}
全部评论

相关推荐

今天 00:59
已编辑
天津师范大学 Web前端
溱元:大四没有实习真的很难了 而且已经是大四下学期了,入行晚了 看样子是刚学完三大件+框架吧 简历真的需要再花时间好好打磨 但是马上又快毕业了 很多公司都要求实习6个月以上 感觉最大的问题就是你没有时间了 虽然可能有点扎心 但就像黑马pink老师说的那样 大四就不要再走it方向了 时间来不及了
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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