题解 | #坐标移动#

坐标移动

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

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

bool isvalued(const string& s){
    if(s.size() == 1) return false;
    if(s.size() > 3) return false;
    if(s[0] != 'A' && s[0] != 'S' && s[0] != 'D' && s[0] != 'W') return false;
    if(!isdigit(s[1])) return false;
    if(s.size() == 3){
    if(!isdigit(s[2])) return false;    
    }
    
    return true;
}

void operation(vector<int>& pos, string& s){
    int juli;
    if(s.size() == 2)
         juli = s[1] - '0';
    else
         juli =(s[1] - '0') * 10 + s[2] - '0';
    char c = s[0];
    if(c == 'A') pos[0] -= juli;
    if(c == 'D') pos[0] += juli;
    if(c == 'W') pos[1] += juli;
    if(c == 'S') pos[1] -= juli;
}


int main() {
    vector<int> pos(2,0);
    string input;
    getline(cin, input);
    string ope = "";
    for(auto i : input){
        if(i != ';') ope += i;
        if(i == ';'){
            if(isvalued(ope)){
            operation(pos, ope);    
            }
            ope.clear();
        }
    }
    cout << pos[0] << ',' << pos[1] << endl;
}

全部评论

相关推荐

喜欢疯狂星期四的猫头鹰在研究求职打法:短作业优先
点赞 评论 收藏
分享
04-11 23:51
门头沟学院 Java
坚定的芭乐反对画饼_许愿Offer版:人人都能过要面试干嘛,发个美团问卷填一下,明天来上班不就好了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务