题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> // 1. 获取一行输入,分段获取指令 // 2. 判断指令是否合法,若合法则移动点 // 3. 输出点坐标 int main() { char input[10010] = {0}; gets(input); char* token = NULL; int x = 0; int y = 0; int mov = 0; for (token = strtok(input, ";"); token; token = strtok(NULL, ";")) { if (strlen(token) == 3) { if (token[0] == 'A' && isdigit(token[1]) && isdigit(token[2])) { mov = atoi(&token[1]); x -= mov; } if (token[0] == 'D' && isdigit(token[1]) && isdigit(token[2])) { mov = atoi(&token[1]); x += mov; } if (token[0] == 'W' && isdigit(token[1]) && isdigit(token[2])) { mov = atoi(&token[1]); y += mov; } if (token[0] == 'S' && isdigit(token[1]) && isdigit(token[2])) { mov = atoi(&token[1]); y -= mov; } } if (strlen(token) == 2) { if (token[0] == 'A' && isdigit(token[1])) { mov = atoi(&token[1]); x -= mov; } if (token[0] == 'D' && isdigit(token[1])) { mov = atoi(&token[1]); x += mov; } if (token[0] == 'W' && isdigit(token[1])) { mov = atoi(&token[1]); y += mov; } if (token[0] == 'S' && isdigit(token[1])) { mov = atoi(&token[1]); y -= mov; } } } printf("%d,%d", x, y); return 0; }