當前位置:編程學習大全網 - 編程語言 - python猴子問題求代碼

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))

  • 上一篇:沒有基礎可以學習軟件開發嗎?
  • 下一篇:什麽是ASP?
  • copyright 2024編程學習大全網