题解 | #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
全部评论

相关推荐

牛客84809583...:举报了
点赞 评论 收藏
分享
迟缓的斜杠青年巴比Q...:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-29 17:30
找实习找着找着就要进入7月了,马上秋招也要开始了,找实习还有意义吗?
绝迹的星:有面就面, 没面上就当日薪4位数大佬免费培训, 面上了再考虑要不要实习
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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