题解 | #坐标移动#

坐标移动

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

#include<iostream>
#include<string>
using namespace std;
static int x ;
static int y ;
void change(string&a,int start,int end)
{
    int go=0;
    if(a[start]=='A'||a[start]=='W'||a[start]=='S'||a[start]=='D')
    {
        if(a[start]=='A')go=1;
        else if(a[start]=='D')go=2;
        else if(a[start]=='W')go=3;
        else go=4;
        start++;
        string num="";
        while(start<end)
        {
            if(!(a[start]>='0'&&a[start]<='9'))
                return;
            else
            {
                num+=a[start];
                start++;
            }
        }
        int n=atoi(num.c_str());
        switch(go){
            case 1:
                x-=n;
                break;
            case 2:
                x+=n;
                break;
            case 3:
                y+=n;
                break;
            case 4:
                y-=n;
                break;                 
        }
        
    }
    else
    {
        return;
    }
    
}
int main(){
    string input;
    getline(cin, input);
    for(int i =0;i<input.size();)
    {
        for(int j =i;j<input.size();j++)
        {
            if(input[j]==';')
            {
                change(input,i,j);
                i=j+1;
            }
        }
    }
    cout<<x<<','<<y<<endl;
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务