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

熱點內容
51單片機控制三相控制器 瀏覽:809
手機上什麼解壓軟體可以強制解壓 瀏覽:781
win7有自帶編譯器嗎 瀏覽:541
轉接器連了沒有文件夾 瀏覽:570
二手開利螺桿壓縮機 瀏覽:309
有php基礎學java要多久 瀏覽:300
程序員稅後工資多少可以跳槽 瀏覽:172
個別網站無法解析伺服器的dns地址 瀏覽:972
安卓手機如何打開rmb文件 瀏覽:215
新生兒app叫什麼 瀏覽:65
斗魚加密怎麼弄 瀏覽:761
為什麼會加密不可上網 瀏覽:531
步步高手機編譯時間啥意思 瀏覽:396
程序員復盤app 瀏覽:162
pdf確定 瀏覽:538
php連接mysql埠號 瀏覽:1001
id3演算法在進行某個節點劃分時 瀏覽:408
麥塊伺服器如何登錄正版 瀏覽:687
中國民俗學pdf 瀏覽:387
程序員如何做人力資源 瀏覽:658