题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <limits.h>
#include <stdio.h>
#include <string.h>
int judge(char* str) {
int num = 0;
int len = strlen(str);
if (len == 2 && str[1] >= '0' && str[1] <= '9' ) {
num = str[1] - '0';
return num;
}
if (len == 3 && str[1] >= '0' && str[1] <= '9' && str[2] >= '0' &&
str[2] <= '9') {
if (str[1] != '0' && str[2] != 0) {
num = (str[1] - '0') * 10 + (str[2] - '0');
} else {
num = str[1] - '0';
}
return num;
} else {
return 0;
}
}
int main() {
int x = 0, y = 0;
int len;
char str[10001];
char str_tmp[10001][10];
scanf("%s", str);
len = strlen(str);
int k = 0, j = 0;
for (int i = 0; i < len; i++) {
if (str[i] != ';') {
str_tmp[k][j++] = str[i];
} else {
k++;
j = 0;
}
}
for (int i = 0; i <= k; i++) {
if (str_tmp[i][0] == 'A') {
x -= judge(str_tmp[i]);
} else if (str_tmp[i][0] == 'D') {
x += judge(str_tmp[i]);
} else if (str_tmp[i][0] == 'W') {
y += judge(str_tmp[i]);
} else if (str_tmp[i][0] == 'S') {
y -= judge(str_tmp[i]);
}
}
printf("%d,%d", x, y);
return 0;
}