题解 | #坐标移动#

坐标移动

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

#include <stdio.h>

#include <stdlib.h>

int main() {

    char ch;

    int x = 0;

    int y = 0;

    char input[10000] = {0};

    //scanf("%3s;",input);

    //printf("%s\n",input);

    int iret = 0;

    while ((iret = scanf("%[^;];", input)) != -1) {

        if (input[0] == '\n')

            break;

        if (iret == 0) {

            getchar();

            continue;

        }

        // printf("%d\n",scanf("%[^;];",input));

        //printf("%s\n", input);

        if (strlen(input) == 3) {

            if (input[0] == 'A' && input[1] >= '0' && input[1] <= '9' &&

                    input[2] >= '0' && input[2] <= '9') { //向左移动

                x += -atoi(&input[1]);

            } else if (input[0] == 'D' && input[1] >= '0' && input[1] <= '9' &&

                       input[2] >= '0' && input[2] <= '9') { //向右移动

                x += atoi(&input[1]);

            } else if (input[0] == 'W' && input[1] >= '0' && input[1] <= '9' &&

                       input[2] >= '0' && input[2] <= '9') { //向上移动

                y += atoi(&input[1]);

            } else if (input[0] == 'S' && input[1] >= '0' && input[1] <= '9' &&

                       input[2] >= '0' && input[2] <= '9') { ////向下移动

                y += -atoi(&input[1]);

            } else {

                memset(input, 010000);

                continue;

            }

        } else if (strlen(input) == 2) {

            if (input[0] == 'A' && input[1] >= '0' && input[1] <= '9') { //向左移动

                x += -atoi(&input[1]);

            } else if (input[0] == 'D' && input[1] >= '0' && input[1] <= '9') {

                 //向右移动

                x += atoi(&input[1]);

            } else if (input[0] == 'W' && input[1] >= '0' && input[1] <= '9') { 

                //向上移动

                y += atoi(&input[1]);

            } else if (input[0] == 'S' && input[1] >= '0' && input[1] <= '9') {

                 //向下移动

                y += -atoi(&input[1]);

            } else {

                memset(input, 010000);

                continue;

            }

        } else {

            memset(input, 010000);

            continue;

        }

        memset(input, 010000);

    }

    printf("%d,%d\n", x, y);

    return 0;

}

全部评论

相关推荐

点赞 评论 收藏
分享
01-18 09:26
已编辑
门头沟学院 Java
王桑的大offer:建议中间件那块写熟悉即可,写掌握 面试包被拷打到昏厥
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务