python猴子函數(shù) python中的函數(shù)

python類約瑟夫環(huán)原創(chuàng)問題求解 求大神

#?totalNum:猴子總數(shù)

成都創(chuàng)新互聯(lián)主要從事網(wǎng)頁設(shè)計、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機版網(wǎng)站建設(shè))、成都響應(yīng)式網(wǎng)站建設(shè)公司、程序開發(fā)、網(wǎng)站優(yōu)化、微網(wǎng)站、微信平臺小程序開發(fā)等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)站設(shè)計、網(wǎng)絡(luò)營銷經(jīng)驗,集策劃、開發(fā)、設(shè)計、營銷、管理等多方位專業(yè)化運作于一體。

#?startNum:開始序號

#?intervalNum:間隔數(shù)

def?KingElect(totalNum,?startNum,?intervalNum):

monkeyList?=?[]

out_order?=?0??#?出列排序

current_index?=?0??#?當(dāng)前列表下標

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的單向循環(huán)鏈實現(xiàn)猴子選大王

count?=?10?#?總猴子數(shù)

i?????=?0??#?開始的序號

skip??=?1??#?報數(shù)的間隔數(shù)

arr???=?list(range(count))?#?創(chuàng)建一個列表

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

#?輸出結(jié)果:

#?[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 對應(yīng) 男、女、中性。

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

網(wǎng)站標題:python猴子函數(shù) python中的函數(shù)
文章鏈接:http://www.muchs.cn/article42/hjegec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、搜索引擎優(yōu)化、、Google、虛擬主機、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)