题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h>
#include <stdlib.h>
int main() {
char ch;
int x = 0;
int y = 0;
char input[10000] = {0};
//scanf("%3s;",input);
//printf("%s\n",input);
int iret = 0;
while ((iret = scanf("%[^;];", input)) != -1) {
if (input[0] == '\n')
break;
if (iret == 0) {
getchar();
continue;
}
// printf("%d\n",scanf("%[^;];",input));
//printf("%s\n", input);
if (strlen(input) == 3) {
if (input[0] == 'A' && input[1] >= '0' && input[1] <= '9' &&
input[2] >= '0' && input[2] <= '9') { //向左移动
x += -atoi(&input[1]);
} else if (input[0] == 'D' && input[1] >= '0' && input[1] <= '9' &&
input[2] >= '0' && input[2] <= '9') { //向右移动
x += atoi(&input[1]);
} else if (input[0] == 'W' && input[1] >= '0' && input[1] <= '9' &&
input[2] >= '0' && input[2] <= '9') { //向上移动
y += atoi(&input[1]);
} else if (input[0] == 'S' && input[1] >= '0' && input[1] <= '9' &&
input[2] >= '0' && input[2] <= '9') { ////向下移动
y += -atoi(&input[1]);
} else {
memset(input, 0, 10000);
continue;
}
} else if (strlen(input) == 2) {
if (input[0] == 'A' && input[1] >= '0' && input[1] <= '9') { //向左移动
x += -atoi(&input[1]);
} else if (input[0] == 'D' && input[1] >= '0' && input[1] <= '9') {
//向右移动
x += atoi(&input[1]);
} else if (input[0] == 'W' && input[1] >= '0' && input[1] <= '9') {
//向上移动
y += atoi(&input[1]);
} else if (input[0] == 'S' && input[1] >= '0' && input[1] <= '9') {
//向下移动
y += -atoi(&input[1]);
} else {
memset(input, 0, 10000);
continue;
}
} else {
memset(input, 0, 10000);
continue;
}
memset(input, 0, 10000);
}
printf("%d,%d\n", x, y);
return 0;
}