首页 > 代码库 > 10行Python代码解决约瑟夫环(模拟)

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=013     for loop_i in range(n-1):14         ind = (ind+k)% len(link) 15         ind-=116         print Kill:,link[ind]17         del link[ind]18         if ind==-1: # the last element of link19             ind=020     print survice :,link[0]21    22 23 if __name__ == __main__:24 25     #josephus(100000,300)26     #print ‘-‘*3027     josephus(10,5)28     #print ‘-‘*3029     #josephus(10,1)

 

10行Python代码解决约瑟夫环(模拟)