HJ17题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> using namespace std; int main() { string input; int x = 0, y = 0; getline(cin, input); // input = "A10;S20;W10;D30;X;A1A;B10A11;;A10;"; string tmp; size_t pos = input.find(";"); while (pos != string::npos) { tmp = input.substr(0, pos); input = input.substr(pos + 1); if (tmp.empty()) { pos = input.find(";"); continue; } int size = tmp.size(); if (size != 2 && size != 3) { pos = input.find(";"); continue; } // is = false; string numStr = tmp.substr(1); bool isLeagle = true; int num = 0; for (auto item : numStr) { if (item < '0' || item > '9') { isLeagle = false; } } if (!isLeagle) { pos = input.find(";"); continue; } if (tmp[0] == 'A') { x += atoi(numStr.c_str()) * (-1); } else if (tmp[0] == 'D') { x += atoi(numStr.c_str()) * (1); } else if (tmp[0] == 'W') { y += atoi(numStr.c_str()) * (1); } else if (tmp[0] == 'S') { y += atoi(numStr.c_str()) * (-1); } else { } pos = input.find(";"); } cout << x << "," << y; } // 64 位输出请用 printf("%lld")#刷题记录#