题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> #include <string> #include <vector> using namespace std; bool isD(string str) { int n = str.size(); if (n < 2 && n > 3) return false; if (str[0] != 'A' && str[0] != 'D' && str[0] != 'W' && str[0] != 'S') return false; if (str[1] < '0' || str[1] > '9') return false; if (n == 3) { if (str[2] < '0' || str[2] > '9') { return false; } } return true; } void xy_move(vector<int>& xy, string op) { int distance = stoi(op.substr(1, op.size() - 1)); switch (op[0]) { case 'A': xy[0] -= distance; break; case 'D': xy[0] += distance; break; case 'W': xy[1] += distance; break; case 'S': xy[1] -= distance; break; default: break; } } int main() { string inp; cin >> inp; vector<string> inpData; int i = 0, j = 0; for (; i <= inp.size(); i++) { if (i == inp.size() || inp[i] == ';') { inpData.push_back(inp.substr(j, i - j)); j = i + 1; } } vector<int> xy(2); for (string k : inpData) { if (isD(k)) { xy_move(xy, k); } } cout << xy[0] << "," << xy[1]; } // 64 位输出请用 printf("%lld")
模拟
#华为OD机试真题#华为OD机测试题 文章被收录于专栏
个人练习专栏