题解 | #坐标移动#

坐标移动

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

#include <stdio.h>
#include <string.h>
int main() {
    char op[10000];
    int x = 0, y = 0, len, i, j, num = 0;

    scanf("%s", op);
    len = strlen(op);
    for (i = 0; i < len; i++) {
        j = i + 1;
        num = 0;
        if (op[i] == 'A' || op[i] == 'W' || op[i] == 'D' || op[i] == 'S') {
            while (op[j] != ';') {
                if (op[j] >= '0' && op[j] <= '9') {
                    num = num * 10 + (op[j] - '0');
                    j++;
                } else {
                    break;
                }
                if (op[j] == ';') {
                    if (i < 3) {
                        switch (op[i]) {
                            case 'A':
                                x = x - num;
                                break;
                            case 'D':
                                x = x + num;
                                break;
                            case 'W':
                                y = y + num;
                                break;
                            case 'S':
                                y = y - num;
                                break;
                        }
                    }
                    if (i > 3) {
                        if (op[i - 1] == ';') {
                            switch (op[i]) {
                                case 'A':
                                    x = x - num;
                                    break;
                                case 'D':
                                    x = x + num;
                                    break;
                                case 'W':
                                    y = y + num;
                                    break;
                                case 'S':
                                    y = y - num;
                                    break;
                            }
                        }
                    }
                }
            }
        }
    }

    printf("%d,%d", x, y);
    return 0;
}

全部评论

相关推荐

jack_miller:我给我们导员说我不在这里转正,可能没三方签了。导员说没事学校催的时候帮我想办法应付一下
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务