题解 | #坐标移动#

坐标移动

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

观察一个正确的字符,例如str="A10"

  1. str中非数字的字符只有一个,且其为第一个字符,且其为方向,这里为A;
  2. str中不能只存在一个方向,故str大小大于1。

只处理符合上述标准的字符串即可。

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

auto getSubStr(string& s)
{
    pair<int,int> position={0,0}; //要返回的位置
    int begin=0, end=0, no_num=0, step;//begin:每一步的第一个字符;begin:每一步的最后一个字符;no_num每步里面非数字的个数;step步长
    for(char a:s)
    {
        if(a!=';')
        {
            if(a<47||a>58)
                no_num++;
        }
        else
        {
            if(no_num==1&&end-begin>1)//只有字符串中非数字的字符数为1,且非数字为方向时,且存在步长时 例如:A10
            {
                step=stoi(s.substr(begin+1,end-begin));
                if(s[begin]=='A') position.first-=step;
                else if(s[begin]=='D') position.first+=step;
                else if(s[begin]=='S') position.second-=step;
                else if(s[begin]=='W') position.second+=step;
            }
            begin=end+1;//转到下一步
            no_num=0;
        }
        end++;
    }
    return position;
}
int main()
{
    string s;
    while(cin>>s)
    {
        auto step=getSubStr(s);
        cout<<step.first<<","<<step.second<<endl;
    }
    return 0;

}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
nus2201602...:兄弟,你这个简历撕了丢了吧,就是一坨,去找几个项目,理解项目流程,看几遍就是你的了,看看八股就去干了,多看看牛客里别人发出来的简历,对着写,你这写的啥啊,纯一坨
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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