题解 | #坐标移动#

坐标移动

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

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

void movePosition(char *str, int *x, int *y)
{
    int flag = 0;
    int val = 0;
    if(str[0] == 'A' || str[0] == 'S' || str[0] == 'W' || str[0] == 'D')
    {
        if(str[1] >= '0' && str[1] <= '9')
        {
            if(str[2] == ';')
            {
                val = str[1] - '0';
                flag = 1;
            }
            if(str[2] >= '0' && str[2] <= '9')
            {
                val = (str[1] - '0') * 10 + (str[2] - '0');
                flag = 1;
            }
        }
    }
    if(flag == 1)
    {
        switch(str[0])
        {
            case 'A':
                *x = *x - val;
                break;
            case 'S':
                *y = *y - val;
                break;
            case 'W':
                *y = *y + val;
                break;
            case 'D':
                *x = *x + val;
                break;
            default:
                break;
        }
    }

}

int main(void)
{
    char str[10001];
    int len = 0;
    int i = 0;
    int x = 0, y = 0;
    char str1[4] = {0};
    int cnt = 0;
    while(scanf("%s", str) != EOF)
    {
        len = strlen(str);
        for(i = 0; i < len; i++)
        {
            if(cnt < 5)
            {
                str1[cnt] = str[i];
            }
            cnt++;
            if(str[i] == '\0' || str[i] == ';')
            {
                if(cnt < 5)
                {
                    movePosition(str1, &x, &y);
                }
                memset(str1, 0, 4);
                cnt = 0;
            }
        }
        printf("%d,%d\n", x, y);
    }
    
    return 0;
}
全部评论

相关推荐

10-10 01:10
已编辑
深圳大学 测试开发
面了100年面试不知...:六月到九月,四个项目一个实习,是魔丸吗
投了多少份简历才上岸
点赞 评论 收藏
分享
10-13 12:53
已编辑
湖北工业大学 前端工程师
小海c:包装一下,第一个感觉是字节青训营的那个,后面那个是黑马的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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