题解 | #坐标移动#

坐标移动

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

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

int main(void)
{
    int x = 0, y = 0;
    char str[5000][20] = {'\0'};
    char ch;
    int h = 0, k = 0;

    while ((ch = getchar()) != '\n')
    {
        if (isalnum(ch))
        {
            str[h][k] = ch;
            k++;
        } else if (ch == ';')
        {
            if (k > 3)
            {
                memset(str[h], '\0', sizeof(char) * 20);
            } else if (k == 0)
            {
                continue;
            } else
            {
                h++;
            }
            k = 0;
        }
    }
    char strcp[h][3];
    memset(strcp, '\0', sizeof(char) * h * 3);

    int j = 0;
    for (int i = 0; i < h; ++i)
    {
        if (isdigit(str[i][1]) && !isupper(str[i][2]))
        {
            memcpy(strcp[j], str[i], sizeof(char) * 3);
            j++;
        }
    }
    char (*p)[3];
    p = strcp;
    for (int i = 0; i < j; ++i)
    {
        if (p[i][0] == 'A')
        {
            x -= (p[i][2] == '\0') ? (p[i][1] - '0') : ((p[i][1] - '0') * 10 + (p[i][2] - '0'));
        } else if (p[i][0] == 'D')
        {
            x += (p[i][2] == '\0') ? (p[i][1] - '0') : ((p[i][1] - '0') * 10 + (p[i][2] - '0'));
        } else if (p[i][0] == 'W')
        {
            y += (p[i][2] == '\0') ? (p[i][1] - '0') : ((p[i][1] - '0') * 10 + (p[i][2] - '0'));
        } else if (p[i][0] == 'S')
        {
            y -= (p[i][2] == '\0') ? (p[i][1] - '0') : ((p[i][1] - '0') * 10 + (p[i][2] - '0'));
        }
    }

    printf("%d,%d\n", x, y);
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务