题解 | #MP3光标位置#

MP3光标位置

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

用类来模拟按键操作比较简单

class Player:
    def __init__(self, size):
        self.__cursor = 1
        self.__top = 1
        self.__size = size
    
    def up(self):
        if self.__cursor == 1:
            self.__cursor = self.__size
            self.__top = max(1, self.__size - 3)
        else:
            self.__cursor -= 1
            if self.__cursor < self.__top:
                self.__top -= 1
                
    def down(self):
        if self.__cursor == self.__size:
            self.__cursor = 1
            self.__top = 1
        else:
            self.__cursor += 1
            if self.__cursor > self.__top + 3:
                self.__top += 1
                
    def current_song(self):
        return self.__cursor
    
    def current_list(self):
        top = self.__top
        bottom = self.__top + 3 if self.__size > 4 else self.__size
        return ' '.join([str(k) for k in range(top, bottom+1)])
            
while True:
    try:
        N = int(input().strip())
        operations = input().strip()
        player = Player(N)
        for operation in operations:
            if operation == 'U':
                player.up()
            else:
                player.down()
        print(player.current_list())
        print(player.current_song())
    except:
        break
全部评论

相关推荐

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