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

全部评论

相关推荐

大佬们考的怎么样?看样子不能只刷acm编程题了,机器学习题也要刷下。😭
牛客561236133号:绷不住了,皮什么玩意来着
投递美团等公司10个岗位 >
点赞 评论 收藏
分享
02-23 12:32
已编辑
门头沟学院 嵌入式工程师
King987:学历没有问题,然后既然有实习经历的话,把这个放在上面多写一点,哪怕你自己包装一下,只要能圆回来就行,既然有实习经历的话,肯定主要看实习经历之类的。然后也会主要问这里多准备准备
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务