题解 | #坐标移动#

坐标移动

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

#include <stdio.h>
#include <string.h>

int f(char*a,int l){
    if(l<=3&&l>1){
        if(a[0]=='A'||a[0]=='D'||a[0]=='W'||a[0]=='S'){
            if(l==2){
                if(a[1]>='0'&&a[1]<='9'){
                    return 1;
                }
            }
            else{
                if(a[1]>='0'&&a[1]<='9'&&a[2]>='0'&&a[2]<='9'){
                    return 1;
                }
            }          
        }
    }
    return 0;
}

int main() {
    int x=0,y=0,t;
    int i,j,num=0,len;
    char str[10005],g[10005][105];

    scanf("%s",str);
    j=0;
    for(i=0;str[i]!='\0';i++){
        if(str[i]!=';'){
            g[num+1][j++]=str[i];
        }
        else{
            num++;
            j=0;
        }
    }
    for(i=1;i<=num;i++){
        len=strlen(g[i]);
        if(f(g[i],len)){
            if(len==2){
               t=g[i][1]-'0';
            }
            else{
                t=(g[i][1]-'0')*10+(g[i][2]-'0');
            }
             if(g[i][0]=='A'){
                    x=x-t;
                }
            else if(g[i][0]=='D'){
                    x=x+t;
            }
            else if(g[i][0]=='W'){
                    y=y+t;
            }
            else{
                y=y-t;
            }
        }
    }
    printf("%d,%d",x,y);
    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务