程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Joseph Ring implemented in Python -- who is the actress

編輯:Python

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…")

What is Joseph's ring

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 .

give an example : Which actress can become the heroine

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

The process of the game

  1. Start counting , First count to 3 Of people 3 Number ,3 No. 1 out of circle .

    1, 2,【 3 \color{red}{3} 3】, 4, 5, 6, 7, 8, 9, 10.

  2. from 4 No 1 Start counting , Then count to 3 Of people 6 Number ,6 No. 1 out of circle .

    1, 2,【 3 \color{red}{3} 3】, 4, 5, 【 6 \color{red}{6} 6】, 7, 8, 9, 10.

  3. from 7 No 1 Start counting , Then count to 3 Of people 9 Number ,9 No. 1 out of circle .

    1, 2,【 3 \color{red}{3} 3】, 4, 5, 【 6 \color{red}{6} 6】, 7, 8, 【 9 \color{red}{9} 9】, 10.

  4. 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 \color{red}{2} 2】,【 3 \color{red}{3} 3】, 4, 5, 【 6 \color{red}{6} 6】, 7, 8, 【 9 \color{red}{9} 9】, 10.

  5. from 4 No 1 Start counting , Then count to 3 Of people 7 Number ,7 No. 1 out of circle .

    1, 【 2 \color{red}{2} 2】,【 3 \color{red}{3} 3】, 4, 5, 【 6 \color{red}{6} 6】, 【 7 \color{red}{7} 7】, 8, 【 9 \color{red}{9} 9】, 10.

  6. from 8 No 1 Start counting , Then count to 3 Of people 1 Number ,1 No. 1 out of circle .

    1 \color{red}{1} 1】, 【 2 \color{red}{2} 2】,【 3 \color{red}{3} 3】, 4, 5, 【 6 \color{red}{6} 6】, 【 7 \color{red}{7} 7】, 8, 【 9 \color{red}{9} 9】, 10.

  7. from 4 No 1 Start counting , Then count to 3 Of people 8 Number ,8 No. 1 out of circle .

    1 \color{red}{1} 1】, 【 2 \color{red}{2} 2】,【 3 \color{red}{3} 3】, 4, 5, 【 6 \color{red}{6} 6】, 【 7 \color{red}{7} 7】, 【 8 \color{red}{8} 8】, 【 9 \color{red}{9} 9】, 10.

  8. from 10 No 1 Start counting , Then count to 3 Of people 5 Number ,5 No. 1 out of circle .

    1 \color{red}{1} 1】, 【 2 \color{red}{2} 2】,【 3 \color{red}{3} 3】, 4, 【 5 \color{red}{5} 5】, 【 6 \color{red}{6} 6】, 【 7 \color{red}{7} 7】, 【 8 \color{red}{8} 8】, 【 9 \color{red}{9} 9】, 10.

  9. from 10 No 1 Start counting , Then count to 3 Of people 10 Number ,10 No. 1 out of circle .

    1 \color{red}{1} 1】, 【 2 \color{red}{2} 2】,【 3 \color{red}{3} 3】, 4, 【 5 \color{red}{5} 5】, 【 6 \color{red}{6} 6】, 【 7 \color{red}{7} 7】, 【 8 \color{red}{8} 8】, 【 9 \color{red}{9} 9】, 【 10 \color{red}{10} 10】.

  10. Final surplus 4 Number ,4 Actress No. 1 was elected as the leading lady .

Algorithm implementation

Ideas

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 .

Detailed code and running results

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved