题解 | MP3光标位置
def page(com,n):
#com是指令 n是歌曲数量
song=1#歌曲初始位置
#建立歌单列表
l=[]
flag=0
temp=[1,2,3,4]
for j in range(n):
l.append(int(j)+1)
for i in com:
if n<=4:
if i=='U':
if song==1:
song=l[-1]
temp=l[:4]
else:
song-=1
temp = l[:4]
elif i=='D':
if song==4:
song=l[0]
temp =l[:4]
else:
song+=1
temp = l[:4]
elif n>4:
#滑窗操作区读取
if i=='U':
if song==1:
song=l[-1]
temp=l[-4:]
else:
song-=1
if song < temp[0]:
temp = l[song - 1:song + 3]
if i=='D':
if song==l[-1]:
song=1
temp=l[:4]
else:
song+=1
if song>temp[-1]:
temp = l[song - 4:song]
return song,temp
while True:
try:
n=int(input())
d=list(input())
song,re=page(d,n)
for i in re:
print(i,end=' ')
print( )
print(song)
except:
break
由于只在选择歌曲超过显示范围时会才会刷新屏幕,所以按照在顶端与下端进行讨论很容易就可以得到切片的范围的表达式
不需要额外的表示符号更好理解
建议



