题解 | #坐标移动#

坐标移动

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;
}

全部评论

相关推荐

01-14 16:23
广州商学院 Java
双非后端失败第N人:如果准备好了可以直接投字节,字节是最不看学历的,只要想面,大概率都能给你约面。
双非有机会进大厂吗
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
01-15 20:52
黑皮白袜臭脚体育生:五宿大战是吧,死去的记忆还在攻击我
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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