12门徒问题(即约瑟夫环)
13人围成一圈,从其中一人开始数数,数到3的人踢出去,然后下一个重新从1数,问最后留下哪一个?
def next_one((lista,indexb)): if len(lista)-1>indexb: return (lista,indexb+1) return (lista,0) def j(n,m): a=[] for i in range(1,n+1): a.append(i) count = (a,0) while len(a)>1: for y in range(1,m): count = next_one(count) a.remove(a[count[1]]) return a[0]
n代表围成一圈的人数,m代表数到第几的时候踢人。
返回的是列表a中仅剩的那个元素。
浙公网安备 33010602011771号