思路:选取一个方向数组,判定当前的方向,对应的坐标相加
#include <iostream> #include <string> #include <algorithm> using namespace std; // 定义北东南西四个方向 int g_to[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; const int LOOP = 4; class Solution { public: // 待实现函数,在此函数中填入答题代码; string ExecCommand(const string &commands) const { string result; int direction = 0; int x = 0; int y = 0; for (auto i : commands) { if (i == 'G') { x += g_to[direction][0]; y += g_to[direction][1]; } else if (i == 'L') { direction = (direction + LOOP - 1) % LOOP; } else if (i == 'R') { direction = (direction + LOOP + 1) % LOOP; } } result = "(" + to_string(x) + ","+ to_string(y) + ")"; return result; } }; inline string ReadLine() { string line; getline(cin, line); return line; } int main() { string commands = ReadLine(); Solution solu; string result = solu.ExecCommand(commands); cout << result; return 0; }