题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h>
#include<string.h>
int main() {
int x=0,y=0;
while(1){
char str[4]={'\0'};
if(scanf("%[^;]",str)==EOF){
break;
}
getchar();
if(strlen(str)>3){
continue;
}
if(str[0]!='A'&&str[0]!='W'&&str[0]!='S'&&str[0]!='D'){
continue;
}
if(str[1]>'9'||str[1]<'0'){
continue;//数字只有一位的时候
}
if(strlen(str)==3){
if(str[2]>'9'||str[2]<'0'){
continue;//长度为3,有两位数
}
}
if(str[0]=='A'){
x-=atoi(&str[1]);
}
else if(str[0]=='W'){
y+=atoi(&str[1]);
}
else if(str[0]=='S'){
y-=atoi(&str[1]);
}
else if(str[0]=='D'){
x+=atoi(&str[1]);
}
}
printf("%d,%d\n",x,y);
return 0;
}

查看14道真题和解析