问题遇到的现象和发生背景
列表不是空的,但会弹出IndexError: list assignment index out of range的错误
问题相关代码,请勿粘贴截图
print('输入n:')
n: int = int(input())
print('输入k:')
k: int = int(input())
i: int = 1
a = list()
while i <= n: # 初始化 队
a.append(i)
i += 1
def step1():
global n
j = 1
q = 0
v = n
while v > (k - 2):
while j <= n:
j += 1
if j % k == 0:
q += 1 # 第一回合减去元素的个数
del a[j+1]
v -= 1
n = n - q
return n
def step2():
global n
g = 1
p = 0
x = n
while x > (k - 2):
while g <= n:
g += 1
if g % k == 0:
p += 1 # 第二回合减去元素的个数
del a[n + 1 - g]
x -= 1
n = n - p
return n
def main():
while n > (k - 2):
step1()
step2()
print('输出')
for x in a:
print(x, end=' ')
if name == 'main':
main()