题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h> #include <string.h> int check_key(char a, int* b, int* c) { if(a >= '0' && a <= '9') { int value = a - '0' + 1; return value; } else { switch(a) { case'A': *b = -1; return -1; case'D': *b = 1; return -1; case'W': *c = 1; return -1; case'S': *c = -1; return -1; default: break; } } return 0; } int main() { char s[10000]; scanf("%s", s); int state = 0; int xlabel = 0, ylabel = 0, x = 0, y = 0, reset = 0; int keycount = 0, numcount = 0,num = 0, tmp = 0; for(int i = 0; i < strlen(s); i++) { tmp = check_key(s[i], &xlabel, &ylabel); if(tmp < 0) { keycount++; state = 1; } else if(tmp > 0 && keycount == 1) { state = 2; numcount ++; num *= 10; num += tmp - 1; } else if(s[i] == ';') { if(state == 2 && numcount>= 1 && numcount <= 2 && keycount == 1 && reset == 0) { x += xlabel * num; y += ylabel * num; } xlabel = 0; ylabel = 0; keycount = 0; numcount = 0; num = 0; state = 0; reset = 0; } else{ reset = 1; } } printf("%d,%d",x,y); return 0; }