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

Joseph living and dead games, python implementation

編輯:Python

30 The individual is in a boat , overload , need 15 People get off the ship . So people lined up , The queue is their number .

Number off , from 1 Start , Count to 9 People get off the ship . So circular , Until there's only 15 So far , Ask what number of people got off the ship ?

resolvent :

  1. Save the numbers in a list list_peple=list(range(1,31))
  2. Access the first element , Move first to last ; Repeat this step 8 Time ; The first 9 Delete directly at the time of “ head ” Elements
  3. Repeat the first 2 Step , Until deleted 15 Elements

python Code :

remove_sum=15
remove_num=9
remove_list=[] # Put the number to be moved out in the list 
list_peple=list(range(1,31))# establish 1-30 Number 
r_sum=0 # Number of people moved out 
while r_sum<remove_sum:
r_num=1 # Who are the first people to visit 
while r_num<remove_num:
first=list_peple.pop(0) # Delete the first element of the list and return the deleted element 
list_peple.append(first) # Put the deleted element at the end of the list 
r_num=r_num+1
first=list_peple.pop(0)
remove_list.append(first)# The first 9 individual , Put the deleted element in remove_list At the end of the list 
r_sum+=1

result :

list_peple
[25, 28, 29, 1, 2, 3, 4, 10, 11, 13, 14, 15, 17, 20, 21]
remove_list
[9, 18, 27, 6, 16, 26, 7, 19, 30, 12, 24, 8, 22, 5, 23]


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