题解 | #坐标移动#

坐标移动

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

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

int IsWASD(char str) {
    if (str == 'A' || str == 'S' || str == 'W' || str == 'D')
        return 1;
    else return 0;
}
int UpdateLocation(int oriaddr[2], char dirct, int distce) {
    if (dirct == 'A') {
        oriaddr[0] = oriaddr[0] - distce;
    } else if (dirct == 'D') {
        oriaddr[0] = oriaddr[0] + distce;
    } else if (dirct == 'S') {
        oriaddr[1] = oriaddr[1] - distce;
    } else if (dirct == 'W') {
        oriaddr[1] = oriaddr[1] + distce;
    } else return 0;
    return oriaddr[2];

}

int main() {

    char longstr[10001];
    scanf("%s", &longstr);

    int location[2] = {0, 0};
    for (int i = 0; i < strlen(longstr); i++) {
        if (longstr[i] == ';') continue;
        else {
            if (longstr[i - 1] == ';' || i == 0) { //判断上一位是';'
                if (IsWASD(longstr[i])) { //判断该位是否是‘WASD’
                    if (longstr[i + 1] > 48 &&
                            longstr[i + 1] < 58) { //判断下一位是否>0且<=9是数字1的ASCII:49
                        if (longstr[i + 2] >= 48 && longstr[i + 2] < 58 &&
                                longstr[i + 3] == ';') { //判断之后第二位是数字且第三位是';'
                            location[2] = UpdateLocation(location, longstr[i],
                                                         (longstr[i + 1] - 48) * 10 + longstr[i + 2] - 48);
                            i = i + 3;
                            continue;
                        } else if (longstr[i + 2] == ';') { //判断之后第二位是';'
                            location[2] = UpdateLocation(location, longstr[i], longstr[i + 1] - 48);
                            i = i + 2;
                            // strcpy(longstr,bakupstr);
                            continue;
                        }
                    } else continue;
                }
            } else {
               

            }

        }

    }
   

    printf("%d,%d", location[0], location[1]);
    return 0;
}

感觉屎山代码了》=《,全是if

全部评论

相关推荐

uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
深夜书店vv:腾讯是这样的,去年很多走廊都加桌子当工区
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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