题解 | #MP3光标位置#

MP3光标位置

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

def function2(songlist, cur_window, cur_song, commands):
    commands = list(commands[::-1])
    while commands:
        command = commands.pop()
        if cur_window[cur_song] == songlist[0] and command == 'U':
            cur_window = songlist[-4:]
            cur_song = 3
        elif cur_window[cur_song] == songlist[-1] and command == 'D':
            cur_window = songlist[:4]
            cur_song = 0
        elif cur_song == 0 and cur_window[cur_song] != songlist[0] and command == 'U':
            cur_window = [x-1 for x in cur_window]
            cur_song = 0
        elif cur_song == 3 and cur_window[cur_song] != songlist[-1] and command == 'D':
            cur_window = [x+1 for x in cur_window]
            cur_song = 3
        elif cur_song in [1, 2, 3] and command == 'U':
            cur_song -= 1
        elif cur_song in [0, 1, 2] and command == 'D':
            cur_song += 1
    return [cur_window, cur_song] 

def function1(songlist, cur_window, cur_song, commands):
        commands = list(commands[::-1])
        while commands:
            command = commands.pop()
            if cur_song == 0 and command == 'U':
                cur_song = len(songlist) - 1
            elif cur_song == len(songlist) - 1 and command == 'D':
                cur_song = 0
            elif cur_song in range(1, len(songlist)) and command == 'U':
                cur_song -= 1
            elif cur_song in range(0, len(songlist)-1) and command == 'D':
                cur_song += 1
        return [cur_window, cur_song]
    
while True:
    try:
        n = int(input())
        commands = input()
        songlist = [i for i in range(1, n+1)]
        cur_window = songlist[:4] if n >= 4 else songlist
        cur_song = 0

        if len(songlist) < 4:
            cur_window, cur_song = function1(songlist, cur_window, cur_song, commands)
        elif len(songlist) >= 4:
            cur_window, cur_song = function2(songlist, cur_window, cur_song, commands)
        
        cur_window_str = ''
        for i in range(len(cur_window)):
            if i == 0:
                cur_window_str = str(cur_window[i])
            else:
                cur_window_str = cur_window_str + ' ' + str(cur_window[i])

        print(cur_window_str)
        print(cur_window[cur_song])
    
    except:
        break

            
    

    
    

全部评论

相关推荐

ProMonkey2024:5个oc?厉害! 但是有一个小问题:谁问你了?😡我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了(别的帖子偷来的,现学现卖😋)
点赞 评论 收藏
分享
我见java多妩媚:大外包
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务