题解 | #MP3光标位置# 滑动窗口
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
#include <iostream> using namespace std; void downward(int& win_left, int& win_right, int& cur_pos, int num) { if (cur_pos == win_right) { if (win_right == num) { win_left = 1; win_right = num <= 4 ? num : 4; cur_pos = win_left; } else { ++win_left; ++win_right; ++cur_pos; } } else { ++cur_pos; } } void upward(int& win_left, int& win_right, int& cur_pos, int num) { if (cur_pos == win_left) { if (win_left == 1) { win_left = num <= 4 ? 1 : num - 3; win_right = num; cur_pos = win_right; } else { --win_left; --win_right; --cur_pos; } } else { --cur_pos; } } int main() { int num = 0; cin >> num; string op; cin >> op; int win_left = 1; int win_right = num <= 4 ? num : 4; int cur_pos = 1; for (char c : op) { if (c == 'D') { downward(win_left, win_right, cur_pos, num); } else if (c == 'U') { upward(win_left, win_right, cur_pos, num); } } for (int i = win_left; i <= win_right; ++i) { cout << i << " "; } cout << endl; cout << cur_pos << endl; return 0; }