约瑟夫环问题 Python3实现

Python学习笔记

尝试用Python3实现约瑟夫环问题。

约瑟夫环问题的描述:

约瑟夫斯问题(有时也称为约瑟夫斯置换),是一个出现在计算机科学和数学中的问题。在计算机编程的算法中,类似问题又称为约瑟夫环。
有 n个囚犯站成一个圆圈,准备处决。首先从一个人开始,越过  k-2个人(因为第一个人已经被越过),并杀掉第k个人。接着,再越过 k-1个人,并杀掉第k个人。这个过程沿着圆圈一直进行,直到最终只剩下一个人留下,这个人就可以继续活着。
问题是,给定了n和 k,一开始要站在什么地方才能避免被处决?

假定n=100;k=3。

则正确的答案是:

总共有100个人,每3个人必须被处死一个,则整个被处死的队列是:
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 2 7 11 16 20 25 29 34 38 43 47 52 56 61 65 70 74 79 83 88 92 97 1 8 14 22 28 35 41 49 55 62 68 76 82 89 95 4 13 23 32 44 53 64 73 85 94 5 19 37 50 67 80 98 17 40 59 86 10 46 77 26 71 31 100 58 91

如果最后一个可以存活,那么你应该站在第91个的位置。

现在自己做个程序来验证: 

  1 #!/usr/local/bin/python3
  2 #! version 3.0.7
  3 #! encode utf-8 
  4 n = 100
  5 survive_person = list(range(1,n+1))
  6 count = 3                 
  7 temp = 1
  8 round = 0      
  9 kill_person = []                        
 10 while(round < 10):                      
 11        for i in survive_person :        
 12             print('now it\'s:',i,'turn',end='\n')
 13             if temp == count :          
 14                 kill_person.append(i)   
 15                 survive_person.remove(i)
 16                 temp = 1                
 17                 print('kill person:',i,'lenth of survive:',len(survive_person),end='\n')
 18             temp += 1                   
 19             print('the first turn is ',i,end='\n')
 20        round += 1                       
 21 print(kill_person)
 22 print(survive_person)    

但是答案不对:

$./josephloop.py
now it's: 1 turn
the first turn is  1
now it's: 2 turn
the first turn is  2
now it's: 3 turn
kill person: 3 lenth of survive: 99
the first turn is  3
now it's: 5 turn
the first turn is  5
now it's: 6 turn
kill person: 6 lenth of survive: 98
the first turn is  6
now it's: 8 turn
the first turn is  8
now it's: 9 turn
kill person: 9 lenth of survive: 97
the first turn is  9
now it's: 11 turn
the first turn is  11
now it's: 12 turn
kill person: 12 lenth of survive: 96
the first turn is  12
now it's: 14 turn
the first turn is  14
now it's: 15 turn
kill person: 15 lenth of survive: 95
the first turn is  15
now it's: 17 turn
the first turn is  17
now it's: 18 turn
kill person: 18 lenth of survive: 94
the first turn is  18
now it's: 20 turn
the first turn is  20
now it's: 21 turn
kill person: 21 lenth of survive: 93
the first turn is  21
now it's: 23 turn
the first turn is  23
now it's: 24 turn
kill person: 24 lenth of survive: 92
the first turn is  24
now it's: 26 turn
the first turn is  26
now it's: 27 turn
kill person: 27 lenth of survive: 91
the first turn is  27
now it's: 29 turn
the first turn is  29
now it's: 30 turn
kill person: 30 lenth of survive: 90
the first turn is  30
now it's: 32 turn
the first turn is  32
now it's: 33 turn
kill person: 33 lenth of survive: 89
the first turn is  33
now
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值