题解 | #MP3光标位置#

MP3光标位置

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

#include <iostream>
using namespace std;

int main() {
    int n;
    string cmd;
    while(cin>>n>>cmd)
    {
        int num=1,first=1;
        if(n<=4)
        {
            //总数小于等于4
            for(int i=0;i<cmd.size();++i)
            {
                if(num==1&&cmd[i]=='U')
                num=n;
                else if(num==n&&cmd[i]=='D')
                num=1;
                else if(cmd[i]=='U')
                num--;
                else
                num++;
            }
            for(int i=1;i<=n;++i)
            {
                cout<<i<<" ";
            }
            cout<<endl;
            cout<<num<<endl;
        }
        else 
        {
            //总数大于4  解析命令
            for(int i=0;i<cmd.size();++i)
            {
                //第一页 第一首歌 向上
                if(first==1&&num==1&&cmd[i]=='U')
                {
                    first=n-3;
                    num=n;
                }
                //最后一页 最后一首歌 向下
                else if(first==n-3&&num==n&&cmd[i]=='D')
                {
                    first=1;
                    num=1;
                }
                //不是第一页 第一首歌 向上
                else if(first!=1&&num==first&&cmd[i]=='U')
                {
                    first--;
                    num--;
                }
                //不是最后一页 最后一首歌 向下
                else if(first!=n-3&&num==first+3&&cmd[i]=='D')
                {
                    first++;
                    num++;
                }
                else if(cmd[i]=='U')
                    num--;
                else
                num++;
            }
            for(int i=first;i<=first+3;++i)
            {
                cout<<i<<" ";
            }
            cout<<endl;
            cout<<num<<endl;
        }
    }
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 13:05
TMD找工作本来就烦,这东西什么素质啊😡
Beeee0927:hr是超雄了,不过也是有道理的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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