题解 | #坐标移动#

坐标移动

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

怎么有我这种把代码写的这么罗嗦的人...

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
void** my_malloc(int row, int col, int size)
{
    void** arr = (void**)calloc(row + row * col, size);
    if (NULL != arr)
    {
        char* head;
        head = (char*)arr + row * sizeof(void*);
        while (row--)
        {
            arr[row] = head + row * col * size;
        }
    }
    return arr;
}
void my_free(void** arr)
{
    free(arr);
    arr = NULL;
}
int main()
{

    char** arr1 = my_malloc(1000, 1000, sizeof(char));
    int ret = 0;
    while (1)
    {
        int n = scanf("%[a-z|A-Z|0-9]", arr1[ret]);
        if (n)
        {
            ret++;
        }
        if ('\n' == getchar())
        {
            break;
        }
    }
    int i = 0, j = 0;
    long long x = 0, y = 0;
    for (i = 0; i < ret; i++)
    {
        for (j = 1; j < strlen(arr1[i]); j++)
        {
            if (strlen(arr1[i]) > 3)//无效
            {
                break;
            }
            else if (arr1[i][0] != 'A' && arr1[i][0] != 'D' && arr1[i][0] != 'W' && arr1[i][0] != 'S')//无效
            {
                break;
            }
            if (isdigit(arr1[i][1]))
            {
                if (isdigit(arr1[i][2]) || 2 == strlen(arr1[i]))
                {
                    switch (arr1[i][0])
                    {
                    case 'A':
                    {
                        x -= ((arr1[i][strlen(arr1[i]) - j] - '0') * pow(10, j - 1));
                        break;
                    }
                    case 'D':
                    {
                        x += ((arr1[i][strlen(arr1[i]) - j] - '0') * pow(10, j - 1));
                        break;
                    }
                    case 'W':
                    {
                        y += ((arr1[i][strlen(arr1[i]) - j] - '0') * pow(10, j - 1));
                        break;
                    }
                    case 'S':
                    {
                        y -= ((arr1[i][strlen(arr1[i]) - j] - '0') * pow(10, j - 1));
                        break;
                    }
                    }
                }
            }

        }
    }
    printf("%lld,%lld", x, y);
    my_free(arr1);
    return 0;
}
全部评论

相关推荐

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