導航:首頁 > 編程語言 > 約瑟夫斯環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相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:620
php5314 瀏覽:359
完美國際安裝到哪個文件夾 瀏覽:671
什麼app可以掃一掃做題 瀏覽:542
程序員編碼論壇 瀏覽:927
淘點是什麼app 瀏覽:662
中國高等植物pdf 瀏覽:456
51單片機時間 瀏覽:185
後台如何獲取伺服器ip 瀏覽:269
單片機流水燈程序c語言 瀏覽:237
程序員第二職業掙錢 瀏覽:242
運行里怎麼輸入伺服器路徑 瀏覽:844
pythonstepwise 瀏覽:513
劉一男詞彙速記指南pdf 瀏覽:67
php認證級別 瀏覽:372
方舟編譯啥時候推送 瀏覽:1013
php手機驗證碼生成 瀏覽:678
哲學思維pdf 瀏覽:19
凌達壓縮機有限公司招聘 瀏覽:537
weblogic命令部署 瀏覽:40