Chapter 4 -17. 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
 
1  # Monkeys peaches were problems 
2  # the Author: cnRick 
. 3  # Time: 2020-3-27 
. 4 N = int (INPUT ()) # N is the total number of monkeys 
. 5 LS = [I for I in Range (1, N + 1 )]
 . 6  
. 7 PTR = 1 # from a start number of packets, number of packets monkey subscript -1 
. 8  the while len (LS)> 1 :
 . 9      PTR = PTR + 2
 10      PTR = (PTR-1)% len (LS) . 1 +
 . 11      del LS [-PTR. 1 ]
 12 is  Print (LS [0])

M is for discussion of this question of official links: https://www.icourse163.org/learn/ZJU-1206456840?tid=1450395457#/learn/content?type=detail&id=1214738088&cid=1247847460

Guess you like

Origin www.cnblogs.com/dreamcoding/p/12594015.html