滑动窗口

MP3光标位置

https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15

//
// Created by Administrator on 2024/4/13.
//
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    int all_num;
    cin >> all_num;
    string op;
    cin >> op;

    //初始化滑动窗口和指针
    int ptr = 1, top = 1, bottom;
    if (all_num <= 4)    // 只有一页
        bottom = all_num;
    else
        bottom = 4;     //有多页

    for (const auto &o: op) {
        if (o == 'U') {
            if (ptr != top) //指针在滑动窗口之间
                ptr -= 1;
            else if (ptr == top && top == 1) {  //指针在滑动窗口顶部,且滑动窗口上边界在歌曲列表顶部的位置
                bottom = all_num;
                top = bottom - 3 <= 1 ? 1 : bottom - 3;
                ptr = bottom;
            } else {    //指针在滑动窗口顶部,且滑动窗口在歌曲列表中间
                top -= 1;
                bottom -= 1;
                ptr = top;
            }
        } else if (o == 'D') {
            if (ptr != bottom)  //指针在滑动窗口之间
                ptr += 1;
            else if (ptr == bottom && bottom == all_num) {  //指针在滑动窗口底部,且滑动窗口下边界在歌曲列表底部的位置
                top = 1;
                bottom = top + 3 >= all_num ? all_num : 4;
                ptr = top;
            } else {    //指针在滑动窗口底部,且滑动窗口在歌曲列表中间
                bottom += 1;
                top += 1;
                ptr = bottom;
            }
        }
    }

    for (int i = top; i <= bottom; ++i) //遍历滑动窗口
        cout << i << " ";
    cout << "\n" << ptr;    //输出指针当前的位置


    return 0;
}

全部评论

相关推荐

野猪不是猪🐗:现在的环境就是这样,供远大于求。 以前卡学历,现在最高学历不够卡了,还要卡第一学历。 还是不够筛,于是还要求得有实习、不能有gap等等... 可能这个岗位总共就一个hc,筛到最后还是有十几个人满足这些要求。他们都非常优秀,各方面都很棒。 那没办法了,看那个顺眼选哪个呗。 很残酷,也很现实
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务