题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX 10002
void fun(char* str, int len, int x, int y) {
int flag = 0;
int sig = 0;
int tmp = 0;
for (int i = 0; i < len; i++) {
if (flag == 0) {
if ((str[i] == 'A' || str[i] == 'S' || str[i] == 'D' || str[i] == 'W')) {
flag = 1;
if (str[i] == 'A')
sig = 1;
else if (str[i] == 'S')
sig = 2;
else if (str[i] == 'D')
sig = 3;
else if (str[i] == 'W')
sig = 4;
continue;
} else {
while (str[i] != ';') {
i++;
}
flag = 0;
continue;
}
}
int mi = 0;
while ((str[i] >= '0' && str[i] <= '9') || str[i] == ';') {
if (str[i] >= '0' && str[i] <= '9') {
tmp = tmp * pow(10, mi++) + (str[i] - '0');
i++;
} else {
if (sig == 1)
x = x - tmp;
else if (sig == 2)
y = y - tmp;
else if (sig == 3)
x = x + tmp;
else if (sig == 4)
y = y + tmp;
tmp = 0;
flag = 0;
break;
}
}
if (str[i] != ';') {
while (str[i] != ';') {
i++;
}
flag = 0;
tmp = 0;
}
}
printf("%d,%d", x, y);
}
int main() {
char cord[MAX];
fgets(cord, sizeof(cord), stdin);
cord[strlen(cord) - 1] = '\0';
int len = strlen(cord);
int x = 0;
int y = 0;
fun(cord, len, x, y);
return 0;
}
