Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 3 God ,[ Click to see the event details ] (juejin.cn/post/709970… "juejin.cn/post/709970…")
Joseph ring ( Joseph's question ) It's an application of mathematics : It is known that n personal ( To number 1,2,3…n respectively ) Sit around a round table . From the number k People began to count , Count to m The man of the circle ; His next man from 1 Start counting , Count to m The man of the circle ; Repeat this rule , Until the last winner remains .
for example : Yes 10 All the actresses with good acting skills came to run for the heroine of the play , So how to choose , They can play a game , Sit at a round table and play the game , Everyone is numbered 1-10 . If the specified number reaches 3 People out of circles . The game process is as follows .
Pictured
Start counting , First count to 3 Of people 3 Number ,3 No. 1 out of circle .
1, 2,【3】, 4, 5, 6, 7, 8, 9, 10.
from 4 No 1 Start counting , Then count to 3 Of people 6 Number ,6 No. 1 out of circle .
1, 2,【3】, 4, 5, 【6】, 7, 8, 9, 10.
from 7 No 1 Start counting , Then count to 3 Of people 9 Number ,9 No. 1 out of circle .
1, 2,【3】, 4, 5, 【6】, 7, 8, 【9】, 10.
from 10 No 1 Start counting , because 10 I call it ring structure , Then count to 3 Of people 2 Number ,2 No. 1 out of circle .
1, 【2】,【3】, 4, 5, 【6】, 7, 8, 【9】, 10.
from 4 No 1 Start counting , Then count to 3 Of people 7 Number ,7 No. 1 out of circle .
1, 【2】,【3】, 4, 5, 【6】, 【7】, 8, 【9】, 10.
from 8 No 1 Start counting , Then count to 3 Of people 1 Number ,1 No. 1 out of circle .
【1】, 【2】,【3】, 4, 5, 【6】, 【7】, 8, 【9】, 10.
from 4 No 1 Start counting , Then count to 3 Of people 8 Number ,8 No. 1 out of circle .
【1】, 【2】,【3】, 4, 5, 【6】, 【7】, 【8】, 【9】, 10.
from 10 No 1 Start counting , Then count to 3 Of people 5 Number ,5 No. 1 out of circle .
【1】, 【2】,【3】, 4, 【5】, 【6】, 【7】, 【8】, 【9】, 10.
from 10 No 1 Start counting , Then count to 3 Of people 10 Number ,10 No. 1 out of circle .
【1】, 【2】,【3】, 4, 【5】, 【6】, 【7】, 【8】, 【9】, 【10】.
Final surplus 4 Number ,4 Actress No. 1 was elected as the leading lady .
use python List solution :
First use a list to identify this n Personal status , By default, it's all 1 , That is, they are all in the circle , On the count of m After people out of the circle , The logo is set to 0( Just out of the circle ), Clear the alarm at the same time 0, The next person is from 1 Start . Before each count, judge whether he is in the circle ( That is, whether his logo is 1 ), If you are in the circle, you will continue to count off . Define a variable to record the number of people in the circle , The number of people out of the circle is equal to n-1 when , Then the game is over .
def select_actor(n, m):
count = 0 # Record the number of people who have left the circle
num = 0 # Counter
status_list = [] # The status of the players , It all started with 1
for i in range(n):
status_list.append(1)
# Start the game , Number off , Count
while count < n - 1:
for i in range(n):
if status_list[i] == 1:
num +=1
if num==m: # This person counts to m Then the circle , The buzzer is cleared
count += 1
print("%d Withdraw from the election on the th " % (i+1))
status_list[i] = 0
num = 0
if count == n - 1:
break
for idx, val in enumerate(status_list):
if val==1:
print("%d No. 1 qualified to be a leading lady " % (idx+1))
select_actor(10, 3)
Running results