有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第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()
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步