导航:首页 > 编程语言 > 约瑟夫斯环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相关的资料

热点内容
安卓手机怎么把图标全部下移 浏览:183
饥荒被服务器踢出怎么进 浏览:170
c编译器哪款好 浏览:731
快手宝哥发明什么app 浏览:822
张艳玲编译 浏览:66
android展开收起动画 浏览:237
linuxxz文件 浏览:160
在游戏中心里面怎么玩到解压神器 浏览:484
电脑发到手机里面照片怎么解压 浏览:73
虚拟pdf打印机64位 浏览:413
支付宝AES加密和解密 浏览:379
编译实验原理下载 浏览:131
加密防伪溯源系统私人定做 浏览:222
扫码给电动车充电的app叫什么 浏览:760
关闭命令提醒 浏览:356
云账本app服务器 浏览:499
python输入数字循环 浏览:370
未成年人用什么app 浏览:517
程序员出差多久回家 浏览:433
安卓如何更改文件名字 浏览:1002