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

热点内容
丽水四轴加工中心编程 浏览:675
国产系统怎么解压 浏览:552
战双程序员 浏览:483
him触摸编程软件 浏览:931
植物大战僵尸存档怎么转移安卓 浏览:852
java栈的元素 浏览:737
程序员与篮球事件 浏览:675
app反编译不完整 浏览:788
电脑上的文件夹怎么调整 浏览:7
服务器无响应是什么原因呀 浏览:984
wd文档里的app怎么制作 浏览:513
电脑里的文件夹没有了一般能恢复吗 浏览:418
哪里有配加密钥匙的 浏览:210
服务器开不了机怎么把数据弄出来 浏览:958
gif动态图片怎么压缩 浏览:521
黑猴子棒球压缩文件解压密码 浏览:631
如何让app适应不同的手机屏幕大小 浏览:10
苹果手机如何给安卓手机分享软件 浏览:761
苹果电脑怎么运行腾讯云服务器 浏览:59
明日之后沙石堡命令助手 浏览:261