题解 | #坐标移动#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;
}
全部评论

相关推荐

08-07 11:19
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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