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

热点内容
微信公众号图片压缩 浏览:437
可以在安卓平板上画画的软件是什么 浏览:436
高盛数字加密 浏览:895
软着网上能不能查到自己的源码 浏览:914
编译好的android源码 浏览:993
学校机房云服务器和电脑主机 浏览:10
Python红色五角星画法 浏览:337
压缩饼干翻译 浏览:686
macos命令行窗口的样式 浏览:248
androidflipboard 浏览:878
投资公司投资源码 浏览:75
python语料训练 浏览:338
武夷岩茶产地溯源码 浏览:383
求生组队用什么服务器最好 浏览:24
php回调匿名回调函数 浏览:107
源码翻译软件哪里找 浏览:523
邪恶程序员解说 浏览:600
医生找程序员 浏览:423
cad三维剖切命令 浏览:55
压缩机的气能流多远 浏览:85