题解 | #坐标移动#
坐标移动
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;
}