题解 | #MP3光标位置#
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
# 打开题解大家的代码都太长,完全没有看下去的欲望
num = int(input())
move = input()
actions = {'U': -1, 'D': 1}
move_list = []
for i in move:
move_list.append(actions[i])
num_list = [i for i in range(1, num + 1)]
now = 0 # 光标处在全局的位置
loc_now = 1 # 光标处在屏幕(4)的位置
loc_start = 0 #屏幕开始位置
loc_end = 3 #屏幕结束位置
for action in move_list:
if now + action < 0:
now = num - 1
loc_now = 4
loc_start = num - 4
loc_end = num
elif now + action > num-1:
now = 0
loc_now = 1
loc_start = 0
loc_end = 4
else:
now += action
loc_now += action
if loc_now < 1 or loc_now > 4:
loc_now =1 if loc_now < 1 else 4
loc_start += action
loc_end += action
print(' '.join([str(i) for i in num_list[loc_start:loc_end]]))
print(num_list[now])
#1.特殊情况特殊对待,从第一位置往上跳到末尾,从最后位置往下跳到开头
#2.光标位置比较容易判断,只要加上动作数值
#3.难点是屏显窗口内容的变化,定义loc_now记录光标位置在窗口中位置1~4,当loc_now数值在表化过程中超过范围就要进行重置,并移动屏显窗口位置
#
move = input()
actions = {'U': -1, 'D': 1}
move_list = []
for i in move:
move_list.append(actions[i])
num_list = [i for i in range(1, num + 1)]
now = 0 # 光标处在全局的位置
loc_now = 1 # 光标处在屏幕(4)的位置
loc_start = 0 #屏幕开始位置
loc_end = 3 #屏幕结束位置
for action in move_list:
if now + action < 0:
now = num - 1
loc_now = 4
loc_start = num - 4
loc_end = num
elif now + action > num-1:
now = 0
loc_now = 1
loc_start = 0
loc_end = 4
else:
now += action
loc_now += action
if loc_now < 1 or loc_now > 4:
loc_now =1 if loc_now < 1 else 4
loc_start += action
loc_end += action
print(' '.join([str(i) for i in num_list[loc_start:loc_end]]))
print(num_list[now])