题解 | #坐标移动#

坐标移动

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

#include<stdio.h>
#include <string.h>
//用来判断WASD后面接的是否是合法的数字,否则返回0,不影响结果
int func(char *temp){
    int n=0;
    while(*temp!=';'){
        if(*temp>='0'&&*temp<='9'){
            n*=10;
            n+=*temp-'0';
            temp++;
        }
        else{
            return 0;
        }
    }
    return n;
}
int main() {
    int x = 0, y = 0, size = 1;
    char cmd[10001]={0};
    char step[10] = {0};
    scanf("%s", cmd);
    char *p=cmd;
    while(*p!='\0'){
        size=0;
        while(*p!=';'){
            size++;
            p++;
        }
       // printf("size=%d\n",size);
        memset(step,0,sizeof(step));
        strncpy(step,p-size,size+1);
       // printf("%s\n",step);
        switch(step[0]){
            case 'A':x-=func(&step[1]);break;
            case 'W':y+=func(&step[1]);break;
            case 'S':y-=func(&step[1]);break;
            case 'D':x+=func(&step[1]);break;
            default:break;
        }
        p++;
    }    
    printf("%d,%d",x,y);
        
    return 0;
}

全部评论

相关推荐

2025-12-16 17:17
门头沟学院 产品经理
烤点老白薯:他第二句话的潜台词是想让你帮他点个瑞幸或者喜茶啥的
mt对你说过最有启发的一...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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