题解 | #MP3光标位置#

MP3光标位置

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

// 利用数组 array<int,4> dq{0}; 来模拟菜单

#include <array>
#include <deque>
#include <iostream>
using namespace std;

int main() {
    int n;
    string command;
    cin >> n >> command;
    int len  = command.length();
    int pos = 1;
    array<int,4> dq{0};
    if(n <= 4 ){
        for(int i = 0 ; i < len; ++i){
            if(command[i] == 'U'){
                if(--pos == 0){
                    pos = n;
                }
            }else{
                if(++pos == n+1){
                    pos = 1;
                }
            }
        }
        for(int i = 0 ; i < n; ++i){
            cout << i+1 << " ";
        }
        cout << endl;
        cout << pos << endl;
    }
    else{
        //采用滑动窗口
        for(int i = 0 ; i < 4; ++i){
            dq[i] = i + 1;
        }
        for(int i = 0 ; i < len; ++i){
            if(command[i] == 'U'){
                if(--pos == 0){
                    pos = n;
                    //采用滑动窗口
                    for(int j = 0 ; j < 4; ++j){
                        dq[j] = n - 4 + j + 1;
                    }
                }else{
                    if(pos == dq[0]-1){
                        for(int j = 0 ; j < 4; ++j){
                            dq[j] -= 1;
                        }
                    }
                    
                }
            }else{
                if(++pos == n+1){
                    pos = 1;
                    //采用滑动窗口
                    for(int j = 0 ; j < 4; ++j){
                        dq[j] = j + 1;;
                    }
                }else{
                    if(pos == dq[3]+1){
                        for(int j = 0 ; j < 4; ++j){
                            dq[j] += 1;;
                        }
                    }
                    
                }
            }
        }

        for(auto p : dq){
            cout << p << " ";
        }
        cout << endl;
        cout << pos << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 18:13
点赞 评论 收藏
分享
屌丝逆袭咸鱼计划:心态摆好,man,晚点找早点找到最后都是为了提升自己好进正职,努力提升自己才是最关键的😤难道说现在找不到找的太晚了就炸了可以鸡鸡了吗😤早实习晚实习不都是为了以后多积累,大四学长有的秋招进的也不妨碍有的春招进,人生就这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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