题解 | 坐标移动
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define BUFFER_LENGTH (10001)
int get_token_distance(char* token, int token_length) {
int distance = 0;
for (int i=1; i<token_length; i++) {
int temp = token[i] - '0';
if (temp < 0 || temp > 9) {
distance = 0;
break;
}
if (i == 1) {
distance += temp * pow(10, token_length-2);
} else {
distance += temp;
}
}
return distance;
}
int main() {
char buffer[BUFFER_LENGTH];
scanf("%s", buffer);
int x, y = 0;
char* token = strtok(buffer, ";");
while (token != NULL) {
int token_length = strlen(token);
if (token_length == 2 || token_length == 3) {
char direction = token[0];
if (direction == 'A') {
x -= get_token_distance(token, token_length);
} else if (direction == 'D') {
x += get_token_distance(token, token_length);
} else if (direction == 'W') {
y += get_token_distance(token, token_length);
} else if (direction == 'S') {
y -= get_token_distance(token, token_length);
}
}
token = strtok(NULL, ";");
}
printf("%d,%d\n", x, y);
return 0;
}
