题解 | #坐标移动#

坐标移动

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

#include <stdio.h>
#include<string.h>
int main() {
    int x=0,y=0;
    while(1){
        char str[4]={'\0'};
        if(scanf("%[^;]",str)==EOF){
            break;
        }
        getchar();
        if(strlen(str)>3){
            continue;
        }
        if(str[0]!='A'&&str[0]!='W'&&str[0]!='S'&&str[0]!='D'){
            continue;
        }
        if(str[1]>'9'||str[1]<'0'){
            continue;//数字只有一位的时候
        }
        if(strlen(str)==3){
            if(str[2]>'9'||str[2]<'0'){
                continue;//长度为3,有两位数
            }
        }

        if(str[0]=='A'){
            x-=atoi(&str[1]);
        }
        else if(str[0]=='W'){
            y+=atoi(&str[1]);
        }
        else if(str[0]=='S'){
            y-=atoi(&str[1]);
        }
        else if(str[0]=='D'){
            x+=atoi(&str[1]);
        }
    }
    printf("%d,%d\n",x,y);
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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