Joseph ring problem in python

1. Description of the problem

Joseph Ring Problem: It is known that n people (represented by numbers 1, 2, 3...n) are sitting around a round table. Start counting from the person numbered m, and the person who counts to m is killed; his next person starts counting from 1, and the person who counts to m is killed again; repeat this rule until There is only one last person around the round table.

Enter test data

 n = 8

 m =  4

output order

4 8 5 2 1 3 7 6

所以最后一个人是编号为6的人

2. Code part

n=int(input('输入n个人:'))
m =int(input('输入m编号:'))

lis=list(range(1,n+1))
while len(lis)>1:#还可以取模,pop()那个应该删除的元素,注意下标是从0开始的
    #print(lis)
    for i in range(m-1):
        lis.append(lis.pop(0))
    lis.pop(0)
print(lis[0]) 

Guess you like

Origin blog.csdn.net/m0_74459049/article/details/130127373