约瑟夫问题用Python编程解决, Python支持list, 解决这个非常方便:
下面代码是在Python3上调试通过的。
def getLastJoseph(n,m): aList = list(range(1,n+1)) i = 0 j = 1 while(1): if len(aList) == 1: break if i >= len(aList): i = 0 if j == m: a.remove(aList[i]) j = 1 continue j = j + 1 i = i + 1 return aList[0]
def getLastJoseph2(n,m): x = 0 for i in range(2,n+1): x = (x + m) % i return (x + 1)
原文地址:http://blog.csdn.net/caz28/article/details/39234105