题解 | #坐标移动#

坐标移动

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;
}

全部评论

相关推荐

10-21 00:37
已编辑
山东大学 C++
小浪_Coding:你问别人,本来就是有求于人,别人肯定没有义务免费回答你丫, 有点流量每天私信可能都十几,几十条的,大家都有工作和自己的事情, 付费也是正常的, 就像你请别人搭把手, 总得给人家买瓶水喝吧
点赞 评论 收藏
分享
驼瑞驰_招募评论官版...:把对方打入公司库是吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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