题解 | #MP3光标位置#冷静下来读题吧

MP3光标位置

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

n = int(input())
order = input()
lst = []

for i in range(1,n+1):
    lst.append(i)

i = 0
position = 0

for k in order:
    if k == 'U':
        if i == 0:
            i = n-1
            position = min(3,n-1)
        else:
            i -= 1
            if position > 0:
                position -= 1      
    else:
        if i == n-1:
            i = 0
            position = 0
        else:
            i += 1
            if position < 3:
                position += 1


tmp = [lst[i]]
def tmp_add(count,position):
    if position - count < 0 and position + count >min(3,n-1):
        return 
    if position-count > -1:
        tmp.insert(0,lst[(i-count+n)%n])
    if position+count < 4:
        tmp.append(lst[(i+count+n)%n])
    tmp_add(count+1,position)  

tmp_add(1,position)
for j in tmp:
    print(j,end = ' ')
print()
print(lst[i])

全部评论

相关推荐

吴offer选手:学到了,下次面试也放张纸在电脑上,不然老是忘记要说哪几个点
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务