Joseph cicyle's algorithm
约瑟夫环问题:
输入:1)总共人数;2)每次被杀的序号数;
输出:最后一个活着的序号
python代码如下:
n=int (input('please input the number of people:') ) k=int (input('please input the discard number:')) a=[] for i in range(n): a.append(i+1) print 'all the serial number of people:' print a i=0 j=1 while len(a)>1: if j==k: del a[i] if i==len(a): i=0 j=1 else: j=j+1 i=i+1 if i==len(a): i=0 print 'alive number:',a[0]