题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
n = int(input()) cmd = input() def mp4(number, commands): c = 1 l = [1,2,3,4] for i in cmd: if i == 'U': c -= 1 if c not in l and l[0] == 1: l = [n-3,n-2,n-1,n] c = n elif c not in l and l[0] != 1: l = [c, c+1, c+2, c+3] if i == 'D': c +=1 if c not in l and l[3] == n: l = [1,2,3,4] c =1 elif c not in l and l[3] != n: l = [c-3, c-2, c-1, c] print(*l) print(c) def mp3(number, commands): c = 1 l = [] for j in range(n): l.append(j+1) print(*l) for k in cmd: if k == 'U': c -= 1 else: c += 1 c = (c + n) % n print(c) if n > 4: mp4(n, cmd) else: mp3(n, cmd)
首先按照歌曲数目分类:
- 数目n <= 4:列表可以直接输出,用%计算最终位置
- 数目n > 4:判断后输出列表,当前位置随时更新
输出列表情况:
- 指针在列表内:列表不变
- 指针移出当前列表,并移到总列表外:重置
- 指针移出当前列表,但还在总列表内:整体移动当前列表