题解 | #MP3光标位置#

MP3光标位置

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

#include <stdio.h>
#include <string.h>


int main()
{
    int songNum=0;
    scanf("%d",&songNum);
    char commend[109]={0};
    scanf("%s",commend);
    int len=strlen(commend);
    int cursorPosition=1;
    if(songNum<=4)
    {
        for(int i=1;i<=songNum;i++)
        {
            printf("%d ",i);
        }    
        for(int i=0;i<len;i++)
        {
            if(commend[i]=='U'&&cursorPosition==1)
            {
                cursorPosition=songNum;
            }
            else if(commend[i]=='U')
            {
                cursorPosition--;
            }
            else if(commend[i]=='D'&&cursorPosition==songNum)
            {
                cursorPosition=1;
            }
            else if(commend[i]=='D')
            {
                cursorPosition++;
            }
        }
        printf("\n%d",cursorPosition);          
    }
    else if(songNum>4)
    {
        int screenFirstNum=1;
        int screenLastNum=4;
        for(int i=0;i<len;i++)
        {
            if(cursorPosition==1&&commend[i]=='U')
            {
                cursorPosition=songNum;
                screenFirstNum=songNum-3;
                screenLastNum=songNum;
            }
            else if(cursorPosition<screenLastNum&&commend[i]=='D')
            {
                cursorPosition++;
            }
            else if(cursorPosition==screenLastNum&&commend[i]=='D'&&cursorPosition!=songNum)
            {
                screenFirstNum++;
                screenLastNum++;
                cursorPosition++;
            }
            else if(cursorPosition==songNum&&commend[i]=='D')
            {
                cursorPosition=1;
                screenFirstNum=1;
                screenLastNum=4;
            }
            else if(cursorPosition>screenFirstNum&&commend[i]=='U')
            {
                cursorPosition--;
            }
            else if(cursorPosition==screenFirstNum&&commend[i]=='U'&&cursorPosition!=1)
            {
                screenFirstNum--;
                screenLastNum--;
                cursorPosition--;
            }
        }
        printf("%d %d %d %d\n",screenFirstNum,screenFirstNum+1,screenFirstNum+2,screenFirstNum+3);
        printf("%d",cursorPosition);
    }
    return 0;  

}

全部评论

相关推荐

听说改名字就能收到offer哈:Radis写错了兄弟
点赞 评论 收藏
分享
offer多多的六边形战士很无语:看了你的博客,感觉挺不错的,可以把你的访问量和粉丝数在简历里提一下,闪光点(仅个人意见)
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-21 17:16
科大讯飞 算法工程师 28.0k*14.0, 百分之三十是绩效,惯例只发0.9
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务