题解 | #坐标移动#

坐标移动

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;

}

全部评论

相关推荐

05-20 21:57
已编辑
门头沟学院 Java
喜欢吃卤蛋的悲伤蛙在提需求:建信融通没消息吧,我2说有实习挂简历不理了
点赞 评论 收藏
分享
06-25 16:00
武汉大学 Java
工科研究生底薪工资就开3k啊??
机械打工仔:写文章提成的岗位工资低,你怪工科?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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