题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h> #include <string.h> #include <ctype.h> int main(void) { int x = 0, y = 0; char a[10001]; int len; int index = -1; //记录上一次';'的位置 int count = 0; scanf("%s", a); len = strlen(a); for (int i = 0;i < len;i++) { if (a[i] == ';') { count = 0; if ((i - index) > 4 || (i - index) < 3) { //检查两次';'的字符串长度是否满足要求 goto NEXT; } if (i - index == 4) { if (!isdigit(a[i - 2])) goto NEXT; count = (a[i - 2] - '0') * 10; } if (!isdigit(a[i - 1])) goto NEXT; count += a[i -1] - '0'; switch(a[index + 1]) { case 'W': y += count; break; case 'A': x -= count; break; case 'S': y -= count; break; case 'D': x += count; break; } NEXT: index = i; continue; } } printf("%d,%d\n", x, y); return 0; }