题解 | #MP3光标位置#都是细节
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int PAGE_NUM = 4;
int main() {
int n;
cin >> n;
vector<int> i_vec;
for (int i = 1; i <= n; ++i) i_vec.push_back(i);
// 光标默认 歌曲1 在 i_vec 位置
int pos_cursor = 0;
// 默认第 1 页 的显示歌曲在 i_vec 位置
int pos_beg = 0, pos_end = (n < PAGE_NUM) ? (n - 1) : (PAGE_NUM - 1);
string str;
cin >> str;
for (auto item : str) {
if (item == 'U') {
pos_cursor = (pos_cursor - 1 + n) % n;
if (!(pos_cursor >= pos_beg && pos_cursor <= pos_end)) {
pos_end = (pos_beg == 0) ? (n - 1) : (pos_end - 1);
pos_beg = (pos_beg == 0) ? (n - PAGE_NUM) : (pos_beg - 1);
}
}
if (item == 'D') {
pos_cursor = (pos_cursor + 1 + n) % n;
if (!(pos_cursor >= pos_beg && pos_cursor <= pos_end)) {
pos_beg = (pos_end == (n - 1)) ? 0 : (pos_beg + 1);
pos_end = (pos_end == (n - 1)) ? (PAGE_NUM - 1) : (pos_end + 1);
}
}
}
for (int i = pos_beg; i <= pos_end; ++i) cout << i_vec[i] << ' ';
cout << endl;
cout << i_vec[pos_cursor] << endl;
}
// 64 位输出请用 printf("%lld")

查看3道真题和解析