python Chapter 4-18 monkey king election (20 points)

To choose a new group of monkeys Monkey King. Monkey new selection method is: N allow only the candidate circle monkeys, numbered sequentially starting from a certain number from 1 to N position. From the start countin No. 1, from 1 to report 3 per round, where the report of 3 monkeys to exit the circle, and from then immediately begin a monkey under the same number reported. So the cycle, the last remaining elected a monkey on Monkey King. Will the original resolution was elected a few monkey Monkey King?

Input formats:

On a single line to a positive integer N (≤1000).

Output formats:

Monkey King was elected in a row output number.

Sample input:

11

Sample output:

7

Code

n = int(input())
a = []
cnt = 0
#先创建这圈猴子
for i in range(1, n+1):
    a.append(i)
#让他们一个个报数,报到3就踢掉,然后重新开始报数
while len(a) > 1:
    for i in a[:]:
        cnt += 1
        if(cnt == 3):
            a.remove(i)
            cnt = 0
print(a[0])
Released five original articles · won praise 0 · Views 64

Guess you like

Origin blog.csdn.net/Tropine/article/details/104727309