一:解题思路
二:完整代码示例 (C++版和Java版)
C++代码:
#include <iostream> #include <cstring> using namespace std; void selectSong(int nums, const char* str) { int current = 1; int top = 1; int buttom = 4; int len = strlen(str); if (nums <= 4) { current = 1; top = 1; buttom = nums; for (int i = 0; i < len; i++) { if (str[i] == ‘U‘) { if (current == 1) { current = buttom; } else { current--; } } else { if (current == buttom) current = 1; else current++; } } } else { for (int i = 0; i < len; i++) { if (str[i] == ‘U‘) { if (current == 1) { current = nums; top = nums - 3; buttom = nums; } else if (current == top) { current--; top--; buttom--; } else current--; } else { if (current == nums) { current = 1; top = 1; buttom = 4; } else if (current == buttom) { current++; top++; buttom++; } else current++; } } } for (int i = top; i <= buttom; i++) cout << i << " "; cout << endl; cout << current << endl; } int main() { int nums = 0; char str[100] = {}; while (cin >> nums >> str) { selectSong(nums,str); } return 0; }
原文:https://www.cnblogs.com/repinkply/p/13423189.html