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

热点内容
数据库查询系统源码 浏览:617
php5314 浏览:358
完美国际安装到哪个文件夹 浏览:669
什么app可以扫一扫做题 浏览:540
程序员编码论坛 浏览:924
淘点是什么app 浏览:660
中国高等植物pdf 浏览:454
51单片机时间 浏览:182
后台如何获取服务器ip 浏览:267
单片机流水灯程序c语言 浏览:236
程序员第二职业挣钱 浏览:240
运行里怎么输入服务器路径 浏览:843
pythonstepwise 浏览:512
刘一男词汇速记指南pdf 浏览:66
php认证级别 浏览:371
方舟编译啥时候推送 浏览:1012
php手机验证码生成 浏览:677
哲学思维pdf 浏览:17
凌达压缩机有限公司招聘 浏览:535
weblogic命令部署 浏览:39