题解 | #坐标移动#

坐标移动

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

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


// 1. 获取一行输入,分段获取指令
// 2. 判断指令是否合法,若合法则移动点
// 3. 输出点坐标
int main() {

    char input[10010] = {0};
    gets(input);

    char* token = NULL;
    int x = 0;
    int y = 0;
    int mov = 0;
    for (token = strtok(input, ";"); token; token = strtok(NULL, ";")) {
        
        if (strlen(token) == 3) {
            if (token[0] == 'A' && isdigit(token[1]) && isdigit(token[2])) {
                mov = atoi(&token[1]);
                x -= mov;
            }
            if (token[0] == 'D' && isdigit(token[1]) && isdigit(token[2])) {
                mov = atoi(&token[1]);
                x += mov;
            }
            if (token[0] == 'W' && isdigit(token[1]) && isdigit(token[2])) {
                mov = atoi(&token[1]);
                y += mov;
            }
            if (token[0] == 'S' && isdigit(token[1]) && isdigit(token[2])) {
                mov = atoi(&token[1]);
                y -= mov;
            }
        }

        if (strlen(token) == 2) {
            if (token[0] == 'A' && isdigit(token[1])) {
                mov = atoi(&token[1]);
                x -= mov;
            }
            if (token[0] == 'D' && isdigit(token[1])) {
                mov = atoi(&token[1]);
                x += mov;
            }
            if (token[0] == 'W' && isdigit(token[1])) {
                mov = atoi(&token[1]);
                y += mov;
            }
            if (token[0] == 'S' && isdigit(token[1])) {
                mov = atoi(&token[1]);
                y -= mov;
            }
        }

    }

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

    return 0;
}

全部评论

相关推荐

01-26 22:20
已编辑
门头沟学院 Java
Java抽象带篮子:项目很nb了,现在好好准备八股和算法吧,早点找实习,可以看看我的置顶帖子。帖子里写了怎么改简历,怎么包装实习经历,还有2个高质量可速成的项目话术,和我的牛客八股笔记专栏
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务