题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
抄的大佬代码,略有改进,方便上机调试 #include<stdio.h> #include<string.h> #include<stdlib.h> int juge(char *str){ //printf("str:%s\n",str); int len = strlen(str); int num = 0; for (int i = 0; i < len; i++) { if (str[i] >= '0' && str[i] <= '9') { num *= 10; num += (str[i] - '0'); } else{ return 0; } } return num; } int main(){ int x = 0, y = 0, size = 0; char *str = (char *)calloc(1000, sizeof(char)); gets(str); char target[10] = {0}; while (*str != 0) { size = 0; while (*str++ != ';') { size++; } memset(target, 0, sizeof(target)); memcpy(target, str - size - 1, size); //printf("target:%s\n",target); switch (target[0]) { case 'A': x -= juge(&target[1]); break; case 'D': x += juge(&target[1]); break; case 'W': y += juge(&target[1]); break; case 'S': y -= juge(&target[1]); break; default: break; } } printf("%d,%d", x, y); return 0; }