题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h>
#include <string.h>
int main() {
char strTable[10000 ];
scanf("%s",strTable);
char Pos[10];
memset(Pos, 0, sizeof(Pos));
char *pointer = strTable;
int x=0,y=0;
int flag =0;
while (1) {
char *temp = strchr(pointer, ';');
if(temp==NULL) break;
*temp = 0;
if(*(temp+1)==';') {
*(temp+1) = 0;
temp++;
}
strcpy(Pos, pointer);
temp++;
//
pointer = temp;
//
flag = 0;
if(Pos[0]!='W'&&Pos[0]!='A'&&Pos[0]!='S'&&Pos[0]!='D') continue; //首位不是WASD
for (int i=1; i<strlen(Pos); i++) {
if(Pos[i]<'0' || Pos[i]>'9') flag = 1;
}
if(!flag) {
int tempxy = 0;
if(Pos[0]=='W') {
sscanf(Pos,"W%d",&tempxy);
y+=tempxy;
}else if (Pos[0]=='A') {
sscanf(Pos,"A%d",&tempxy);
x-=tempxy;
} else if (Pos[0]=='S') {
sscanf(Pos,"S%d",&tempxy);
y-=tempxy;
}else if (Pos[0]=='D') {
sscanf(Pos,"D%d",&tempxy);
x+=tempxy;
}
}
}
printf("%d,%d",x,y);
return 0;
}

查看3道真题和解析