题解 | #MP3光标位置#

MP3光标位置

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

while True:
    try:
        n = int(input())
        actions = input()
        act_dict = {'U':-1,'D':1} #字典操作加减
        songs = [str(x) for x in range(1,n+1)]  #歌曲列表
        start = 0 #第一格歌曲切片位置
        end = 4   #第4格歌曲切片位置(不含)
        guangbiao = 1 #光标歌曲
        guangbiaoweizhi = 1 #光标在第几格
        if  n <= 4:
            for i in actions:
                guangbiao += act_dict[i]
                if guangbiao < 1:
                    guangbiao = n
                if guangbiao > n:
                    guangbiao = 1
            else:
                print(' '.join(songs))
                print(guangbiao)
        else:
            for i in actions:
                guangbiao += act_dict[i]
                guangbiaoweizhi += act_dict[i]
                if guangbiaoweizhi > 4:
                    guangbiaoweizhi = 4
                    start += act_dict[i]
                    end += act_dict[i]
                    if guangbiao == n+1:
                        guangbiao = 1
                        guangbiaoweizhi =1
                        start = 0
                        end = 4
                if guangbiaoweizhi < 1:
                    guangbiaoweizhi = 1
                    start += act_dict[i]
                    end += act_dict[i]
                    if guangbiao == 0 :
                        guangbiao = n
                        guangbiaoweizhi = 4
                        start = n-4
                        end = n

            else:
                face = songs[start:end]
                print(' '.join(face))
                print(guangbiao)
    except:
        break

        
        
全部评论
#感谢牛主分享,给稍微改造了一下,更易理解 while True: try: n = int(input()) actions = input() act_dict = {'U':-1,'D':1} #字典操作加减 songs = [str(x) for x in range(1,n+1)] #歌曲列表 start = 0 #歌曲列表中第1个歌曲切片位置 end = 4 #歌曲列表中第4个歌曲切片位置(不含) ptrGlobal = 1 #光标在歌曲列表中序号(全局指针) ptrLocal = 1 #光标在当前屏幕上序号(局部指针) if n <= 4: #不大于4首最简单,只考虑一个序号就够了 for i in actions: ptrGlobal += act_dict[i] if ptrGlobal < 1: ptrGlobal = n if ptrGlobal > n: ptrGlobal = 1 else: print(' '.join(songs)) print(ptrGlobal) else: #大于4首稍复杂,要考虑两个序号 for i in actions: ptrGlobal += act_dict[i] ptrLocal += act_dict[i] if ptrLocal > 4: ptrLocal = 4 #将光标在当前屏幕上序号限定在4以内 start += act_dict[i] #小屏幕的起始光标序号要变化 end += act_dict[i] if ptrGlobal == n+1: #超出边界要回归 ptrGlobal = 1 ptrLocal =1 start = 0 end = 4 if ptrLocal < 1: ptrLocal = 1 start += act_dict[i] end += act_dict[i] if ptrGlobal == 0: #超出边界要回归 ptrGlobal = n ptrLocal = 4 start = n-4 end = n else: face = songs[start:end]#小屏幕歌曲范围 print(' '.join(face)) print(ptrGlobal) #打印全局指针 except: break
点赞 回复 分享
发布于 2021-10-30 15:54

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 17:30
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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