题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h> #include <string.h> int IsWASD(char str) { if (str == 'A' || str == 'S' || str == 'W' || str == 'D') return 1; else return 0; } int UpdateLocation(int oriaddr[2], char dirct, int distce) { if (dirct == 'A') { oriaddr[0] = oriaddr[0] - distce; } else if (dirct == 'D') { oriaddr[0] = oriaddr[0] + distce; } else if (dirct == 'S') { oriaddr[1] = oriaddr[1] - distce; } else if (dirct == 'W') { oriaddr[1] = oriaddr[1] + distce; } else return 0; return oriaddr[2]; } int main() { char longstr[10001]; scanf("%s", &longstr); int location[2] = {0, 0}; for (int i = 0; i < strlen(longstr); i++) { if (longstr[i] == ';') continue; else { if (longstr[i - 1] == ';' || i == 0) { //判断上一位是';' if (IsWASD(longstr[i])) { //判断该位是否是‘WASD’ if (longstr[i + 1] > 48 && longstr[i + 1] < 58) { //判断下一位是否>0且<=9是数字1的ASCII:49 if (longstr[i + 2] >= 48 && longstr[i + 2] < 58 && longstr[i + 3] == ';') { //判断之后第二位是数字且第三位是';' location[2] = UpdateLocation(location, longstr[i], (longstr[i + 1] - 48) * 10 + longstr[i + 2] - 48); i = i + 3; continue; } else if (longstr[i + 2] == ';') { //判断之后第二位是';' location[2] = UpdateLocation(location, longstr[i], longstr[i + 1] - 48); i = i + 2; // strcpy(longstr,bakupstr); continue; } } else continue; } } else { } } } printf("%d,%d", location[0], location[1]); return 0; }
感觉屎山代码了》=《,全是if