posts - 0,  comments - 3,  trackbacks - 0

一个农夫养了一头牛,三年后,这头牛每年会生出1头牛,生出来的牛三年后,又可以每年生出一头牛……问农夫10年后有多少头牛?n年呢?(用JAVA实现

1.
Java code
public class Cow { static int count = 1; private static void feedCow(int year,int age){ year++; age++; if(year<=30){ if(age>=3){ count++; feedCow(year,0); } feedCow(year,age); } } public static void main(String[] args) { new Cow().feedCow(0, 0); System.out.println(count); } }


2.
Java code
public class Cow { public static int count = 0; public Cow(int year){ count++; for(int i=3+year;i<=10;i++){ new Cow(i); } } public static void main(String[] args) { new Cow(0); System.out.println(count); } }
posted on 2009-11-03 20:41 赵永亮 阅读(187) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

留言簿

文章档案

搜索

  •  

最新评论