python猴子函数 python中的函数

python类约瑟夫环原创问题求解 求大神

# totalNum:猴子总数

成都创新互联主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、成都响应式网站建设公司、程序开发、网站优化、微网站、微信平台小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都网站制作、成都做网站、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。

# startNum:开始序号

# intervalNum:间隔数

def KingElect(totalNum, startNum, intervalNum):

monkeyList = []

out_order = 0  # 出列排序

current_index = 0  # 当前列表下标

if (totalNum  intervalNum):

return

monkeyId = startNum  # 猴子初始排列

for i in range(1, totalNum + 1):

if monkeyId == 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], 'Gain the elect')

if __name__ == '__main__':

KingElect(60, 1, 2)

如何用python的单向循环链实现猴子选大王

count = 10 # 总猴子数

i     = 0  # 开始的序号

skip  = 1  # 报数的间隔数

arr   = list(range(count)) # 创建一个列表

print(arr) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

while(count  1):

if i = count: i = i % count

print('删除:', arr[i], end = ' ')

arr.pop(i)

print('剩余:', arr)

count = len(arr)

i = i + skip

# 输出结果:

# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# 删除: 0 剩余: [1, 2, 3, 4, 5, 6, 7, 8, 9]

# 删除: 2 剩余: [1, 3, 4, 5, 6, 7, 8, 9]

# 删除: 4 剩余: [1, 3, 5, 6, 7, 8, 9]

# 删除: 6 剩余: [1, 3, 5, 7, 8, 9]

# 删除: 8 剩余: [1, 3, 5, 7, 9]

# 删除: 1 剩余: [3, 5, 7, 9]

# 删除: 5 剩余: [3, 7, 9]

# 删除: 9 剩余: [3, 7]

# 删除: 7 剩余: [3]

python猴子问题求代码

公猴子群:

monkeyMale = []

母猴子群:

monkeyFemale = []

中性猴子群:

monkeyXman = []

class monkey():

def __init__(self, id, adad, amum, abirth):

self.id = id

self.dad = adad

self.mum = amum

self.birth = abirth

self.couple = -1

def dealDie(self, aYear, aBoy):

xman = aYear - self.birth

xman == 60

如果死了,就放开配偶。

公的放母的:

monkeyFemale[self.couple].couple = -1

母的放公的:

monkeyMale[self.couple].couple = -1

def yieldBaby(self, aYear): # 母猴生猴子

x = aYear - self.birth #计算出母猴子周岁。

if x = 19 and x =59: #生孩子

if self.couple == -1: #要个对象(一对夫妇就生一个孩子)。

for i in monkeyMale:

xman = aYear - i.birth

if (i.couple == -1) and (xman = 19 and xman =59): # 找个没对象的,20岁到60岁的。

self.couple = i.id #互相配偶

i.couple = self.id

break

if self.couple = 0: #找到对象了。

第2题:if x % 10 == 0:

龙凤 = True # 10年,就生龙凤胎

else

龙凤 = False

第3题:if x % 3 == 1/2/0 对应 男、女、中性。

if 男孩 or 龙凤:

newId = len(monkeyMale)

monkeyMale.append(monkey(newId, self.couple, self.id, aYear))

if 女孩 or 龙凤:

newId = len(monkeyFemale)

monkeyFemale.append(monkey(newId, self.couple, self.id, aYear))

if 中性

newId = len(monkeyXman)

monkeyXman.append(monkey(newId, self.couple, self.id, aYear))

上来有个公猴和母猴

monkeyMale.append(monkey(0, -1, -1, 1, True))

monkeyFemale.append(monkey(0, -1, -1, 1, True))

从第1年到第100年。

for iYear in range(1,101):

print("year" + str(iYear))

for i in monkeyFemale: #先生孩子

i.yieldBaby(iYear)

for i in monkeyMale: #公猴子die

i.dealDie(iYear, True)

for i in monkeyFemale: #母猴子die

i.dealDie(iYear, False)

# 中性就没用

for i in monkeyFemale:

print(u"母猴id:%s,父%s,母%s,生于%s" % (i.id, i.dad, i.mum, i.birth))

for i in monkeyMale:

print(u"公猴id:%s,父%s,母%s,生于%s" % (i.id, i.dad, i.mum, i.birth))

for i in monkeyXman:

print(u"中性猴id:%s,父%s,母%s,生于%s" % (i.id, i.dad, i.mum, i.birth))


文章标题:python猴子函数 python中的函数
URL分享:http://scjbc.cn/article/hjegec.html

其他资讯