10行Python代码解决约瑟夫环(模拟)
http://blog.csdn.net/dengyaolongacmblog/article/details/39208675
1 #!/usr/bin/env python 2 # coding: utf-8 3 4 import os 5 import sys 6 import string 7 import operator 8 import re 9 10 def josephus(n,k): 11 link=range(1,n+1) 12 ind=0 13 for loop_i in range(n-1): 14 ind = (ind+k)% len(link) 15 ind-=1 16 print 'Kill:',link[ind] 17 del link[ind] 18 if ind==-1: # the last element of link 19 ind=0 20 print 'survice :',link[0] 21 22 23 if __name__ == '__main__': 24 25 #josephus(100000,300) 26 #print '-'*30 27 josephus(10,5) 28 #print '-'*30 29 #josephus(10,1)
一个奔跑的程序员