導航:首頁 > 編程語言 > 約瑟夫斯環python

約瑟夫斯環python

發布時間:2022-07-18 08:39:31

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)

閱讀全文

與約瑟夫斯環python相關的資料

熱點內容
oa伺服器異常怎麼辦 瀏覽:68
cmd編譯utf8 瀏覽:276
怎麼截取app接受的數據 瀏覽:276
nrf24l01pdf 瀏覽:298
php字元串轉array 瀏覽:434
U盤分了文件夾後 瀏覽:940
javasetstring 瀏覽:837
壓縮包里文件夾是白色的 瀏覽:472
編譯鏈接知乎 瀏覽:591
php查詢按鈕 瀏覽:715
有音響游戲解壓神器 瀏覽:253
怎麼壓縮圖片jpeg 瀏覽:713
澳大利亞net程序員 瀏覽:579
程序員加班難受 瀏覽:990
如何看伺服器品牌 瀏覽:256
ecy50clp壓縮機多少W 瀏覽:755
mac終端命令怎麼保存 瀏覽:850
微信公眾號圖片壓縮 瀏覽:440
可以在安卓平板上畫畫的軟體是什麼 瀏覽:438
高盛數字加密 瀏覽:897