题解 | #坐标移动#

#include <iostream>
#include <string>
#include<unordered_map>
using namespace std;

unordered_map<char,pair<int,int>> mp={
    {'A',{-1,0}},
    {'W',{0,1}},
    {'S',{0,-1}},
    {'D',{1,0}}
};

//执行移动命令
void doCommand(pair<int,int>& p,string& command){
    char comm='\0';
    if(mp.count(command[0])){
        comm=command[0];
    }
    if(comm=='\0') return;//首字母必须要是A、W、D、S
    int i=1,n=command.size(),num=0;
    while(i<n&&command[i]>='0'&&command[i]<='9'){
        num=num*10+command[i]-'0';
        i++;
    }
    if(i!=n) return;//末尾必须要是数字
    p.first+=mp[comm].first*num;
    p.second+=mp[comm].second*num;
}

int main(){
    string s;
    getline(cin,s);
    int i=0,n=s.size();
    pair<int,int> point;
    while(i<n){
        int start=i;
        while(i<n&&s[i]!=';') i++;
        string command=s.substr(start,i-start);//读取出命令
        doCommand(point,command);//执行命令
        i++;
    }
    printf("%d,%d",point.first,point.second);
}
#在线刷题#
全部评论

相关推荐

05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
爱吃肉的伊登在写日记:好棒,27届简历能做成这个样子,但是第一个项目感觉cover住难度还是不小的,特别是二面的时候肯定要对分布式系统设计这一块儿有高出正常面试者的水平才行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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