㈠ python约瑟夫环怎么判
importcollections
defjoseph(lst,k):
t=collections.deque(lst)
whilelen(t)>1:
t.rotate(-k)
t.popleft()
returnt
㈡ Python语言之如何实现约瑟夫环问题
def josephus(n, m):
if type(n) != type(1) or n <= 0:
raise Exception('n must be an integer(n > 0)')
if n == 1:
return 0
else:
return (josephus(n - 1, m) + m) % n
if __name__ == '__main__':
print josephus(8, 3)
print josephus(1, 2)
print josephus(0, 2)
㈢ python类约瑟夫环原创问题求解 求大神
#totalNum:猴子总数
#startNum:开始序号
#intervalNum:间隔数
defKingElect(totalNum,startNum,intervalNum):
monkeyList=[]
out_order=0#出列排序
current_index=0#当前列表下标
if(totalNum<intervalNum):
return
monkeyId=startNum#猴子初始排列
foriinrange(1,totalNum+1):
ifmonkeyId==totalNum+1:
monkeyId=1
monkeyList.append(monkeyId)
monkeyId+=1
#print(monkeyList,end='')
while(len(monkeyList)>1):
out_order+=1
current_index+=1
if(current_index>len(monkeyList)):
current_index=1
if(out_order==intervalNum):
intervalNum+=1
out_order=0
print('--',monkeyList[current_index-1],'Out')
monkeyList.pop(current_index-1)
print(end='')
current_index-=1
print('--',monkeyList[0],'Gaintheelect')
if__name__=='__main__':
KingElect(60,1,2)