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

复制代码
#-*-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()
    
复制代码

 

posted @   UCanBeFree  阅读(2507)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示