题解 | #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")

全部评论

相关推荐

11-13 10:17
门头沟学院 Java
昨天面美团,jvm,juc问的好深啊,感觉小林coding不太够喔,牛油们有没有什么推荐的八股网站嘛🕒&nbsp;岗位/面试时间👥&nbsp;面试题目🤔&nbsp;面试感受
明天不下雨了:小林Coding:https://xiaolincoding.com/ 全栈哥:https://www.pdai.tech/ Guide哥:https://javaguide.cn/ 秀哥:https://interviewguide.cn/ 沉默王二:https://javabetter.cn/home.html 磊哥:https://www.javacn.site/interview/basic/ 小傅哥:https://bugstack.cn/ 源码哥:https://doocs.github.io/source-code-hunter/#/ 各大厂的公众号技术文章和一些经典的书籍
面试太紧张了怎么办?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务