题解 | #MP3光标位置#

MP3光标位置

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

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n=0;
    cin>>n;
    string str;
    cin>>str;
    int h=1;
    int s=str.length();
    int head=1;//当前列表头
    int rear=4;//当前列表尾

    //遍历最终光标位置
    for(char c : str)
    {
        //向上特殊翻页
        if(c=='U'&&h==1)
        {
            h=n;
            head=n-3;
            rear=n;
            continue;
        }

        //一般向上走
        if(c=='U'&&h!=1)
        {
            //当前光标位置是列表第一个,需要往上翻一个
            if(head==h)
            {
                head--;
                rear--;
            }
            //光标动
            h--;
            continue;
        }

        //向下特殊翻页
        if(c=='D'&&h==n)
        {
            h=1;
            head=1;
            rear=4;
            continue;
        }

        //一般向下走
         if(c=='D'&&h!=n)
        {
            //当前光标位置是列表最后一个,需要往下翻一个
            if(rear==h)
            {
                head++;
                rear++;
            }
            //光标动
            h++;
            continue;
        }
    }

    //num<=4 不用翻页
    if(n<=4)
    {
        for(int i=1;i<=n;i++)
        {
        cout<<i<<' ';
        }
        cout<<endl<<h;
        
        return 0;
    
    }

    //num>4 可能需要翻页
    else
    {
        cout<<head<<' '<<head+1<<' '<<head+2<<' '<<head+3<<endl<<h;
        return 0;
    }
}

全部评论

相关推荐

10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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