华为机考真题 | 机器人移动题解 | 坐标移动

坐标移动

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

#include <iostream>
#include<bits/stdc++.h>
#include<string.h>
using namespace std;

    bool isNum (string s){
        int len = s.size();
        for (int i = 0;i<len;i++){
            if('0' <= s[i]&& s[i] <= '9')
            continue;
            else
            return false;
        }
        return true;
    }
        int main() {
            string s;
    pair<int,int> p(0,0);        
    while (getline(cin,s,';')){
   if(s.empty())
   continue;
   if(s.size()<2)
   continue;
   
   string s1  = s.substr(1);
   if(isNum(s1)){
    switch(s[0]){
        case 'A': p.first-=stoi(s1);
        break;
        case 'D': p.first+=stoi(s1);
        break;
        case 'W': p.second+=stoi(s1);
        break;
        case 'S': p.second-=stoi(s1);
        break;
        default:
        break;
    }
   }
    }
    cout<<p.first<<","<<p.second<<endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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