题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
import sys try: n, action = 0, '' while True: line = sys.stdin.readline().strip() if line == '': break lines = line.split() if n == 0: n = int(lines[0]) else: action = lines[0] actions = list(action) # 定义当前光标 cur = 1 # 默认窗口 win = [1, 2, 3, 4] # 上一次光标所在的位置 pre = 1 for ac in actions: if ac == 'U': cur = n if cur == 1 else cur - 1 if ac == 'D': cur = 1 if cur == n else cur + 1 # 首页和尾页的转换 if pre == n and cur == 1: win = [1, 2, 3, 4] if pre == 1 and cur == n: win = [n-3, n-2, n-1, n] pre = cur # 如果当前编号不在窗口里面,则往窗口添加 if cur not in win: win.append(cur) win.sort() # 保持窗口大小为4,根据操作UD来判断从 首或者尾 删除元素 if ac == 'U': win.pop() if ac == 'D': win.pop(0) # 如果歌曲数量小于4 if n < 4: win = [i + 1 for i in range(n)] page = [str(num) for num in win] print(' '.join(page)) print(cur) except: pass