题解 | #坐标移动#

坐标移动

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

#include<stdio.h>

#define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))

int Str2Num(const char *str, int *num)
{
    int n = 0;

    for (; *str; str++) {
        if (!IS_DIGIT(*str))
            return -1;
        n *= 10;
        n += *str - '0';
    }
    *num = n;
    return 0;
}

void Move(const char *cmd, int len, int *x, int *y)
{
    int num;

    if (len > 3 || len < 2)
        return;
    if (Str2Num(cmd + 1, &num) < 0)
        return;
    switch (cmd[0]) {
        case 'W':
            *y += num;
            break;
        case 'S':
            *y -= num;
            break;
        case 'D':
            *x += num;
            break;
        case 'A':
            *x -= num;
            break;
    }
}

void Parse(char *str)
{
    int i, x = 0, y = 0;

    for (i = 0; str[i]; i++) {
        if (str[i] != ';')
            continue;
        str[i] = 0;
        Move(str, strlen(str), &x, &y);
        str = str + i + 1;
        i = -1;
    }
    printf("%d,%d\n", x, y);
}

int main()
{
    char buf[10001];

    while (gets(buf) != NULL)
        Parse(buf);
    return 0;
}
全部评论

相关推荐

10-22 12:03
山东大学 Java
程序员小白条:26届一般都得有实习,项目可以随便写的,如果不是开源社区的项目,随便包装,技术栈也是一样,所以本质应该找学历厂,多投投央国企和银行,技术要求稍微低一点的,或者国企控股那种,纯互联网一般都得要干活
应届生简历当中,HR最关...
点赞 评论 收藏
分享
10-13 13:49
南京大学 财务
饿魔:笑死我了,你简直是个天才
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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