题解 | #MP3光标位置#

MP3光标位置

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

#include <cstring>
#include <iostream>
using namespace std;

int main() 
{
    int curList[4]={1,2,3,4}; //保存当前歌曲列表
    int num;
    cin>>num;
    int*arr=new int[num];
    for(int i=0;i<num;++i)
        arr[i]=i+1; //初始化歌曲列表
    int cursor=0; //光标位置
    char*order=new char[100];
    memset(order, 0, 100);
    cin>>order;
    while(*order)
    {
        char c=*order;
        if(c=='U')
        {
            if(num>=4)
            {
                if(arr[cursor]==curList[0]) //光标位于歌曲列表头
                {
                    if(cursor==0)
                    {
                        cursor=num-1;
                        curList[0]=arr[num-4];
                        curList[1]=arr[num-3];
                        curList[2]=arr[num-2];
                        curList[3]=arr[num-1];
                    }
                    else 
                    {
                        curList[0]=arr[cursor-1];
                        curList[1]=arr[cursor];
                        curList[2]=arr[cursor+1];
                        curList[3]=arr[cursor+2];
                        --cursor;
                    }
                }
                else
                    --cursor;
            }
            else
            {
                if(cursor==0)
                    cursor=num-1;
                else
                    --cursor;
            }
        }
        else if(c=='D')
        {
            if(num>=4)
            {
                if(arr[cursor]==curList[3]) //光标位于歌曲列表尾
                {
                    if(cursor==num-1)
                    {
                        cursor=0;
                        curList[0]=arr[0];
                        curList[1]=arr[1];
                        curList[2]=arr[2];
                        curList[3]=arr[3];
                    }
                    else 
                    {
                        curList[0]=arr[cursor-2];
                        curList[1]=arr[cursor-1];
                        curList[2]=arr[cursor];
                        curList[3]=arr[cursor+1];
                        ++cursor;
                    }
                }
                else
                    ++cursor;
            }
            else 
            {
                if(cursor==num-1)
                    cursor=0;
                else
                    ++cursor;
            }
        }
        ++order;
    }

    if(num<=4)
    {
        for(int i=0;i<num;++i)
            cout<<arr[i]<<" ";
    }
    else 
    {
        for(int i=0;i<4;++i)
            cout<<curList[i]<<" ";
    }
    cout<<"\n";
    cout<<cursor+1;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务