您的位置:首页 > 编程语言

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

2014-07-07 12:27 597 查看
#-*-coding:utf-8-*-
'''
Created on 2014年7月7日

@author: xyt
'''

'''尝试抽象一个牛群'''
class Cows():
def __init__(self,age):
self.age=age
self.count_age_1=0#1岁奶牛
self.count_age_2=0#2岁奶牛
self.count_age_3=0#3岁奶牛
self.count_age_all=1#成年奶牛

def countCow(self):
for i in range(1,self.age):
self.count_age_all=self.count_age_3+self.count_age_all
self.count_age_3=self.count_age_2
self.count_age_2=self.count_age_1#
self.count_age_1=self.count_age_all#有多少成年奶牛 过一年就会有多少1岁奶牛
print "1岁奶牛:"+str(self.count_age_1)
print "2岁奶牛:"+str(self.count_age_2)
print "3岁奶牛:"+str(self.count_age_3)
print "成年奶牛:"+str(self.count_age_all)
print "第"+str(i)+"年总共:"+str(self.count_age_1+self.count_age_2+self.count_age_3+self.count_age_all)

return self.count_age_1+self.count_age_2+self.count_age_3+self.count_age_all

'''How many Cows do you have'''
def countCow(age):
pass

if  __name__=='__main__':
sum=Cows(7)
print sum.countCow()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐