一头母牛,三年后,这头母牛每年会生出1头母牛, 生出来的母牛三年后,又可以每年生出一头母牛
2013-04-28 15:27
344 查看
该题是蓝桥杯上的一道填空题。应该是为了考察集合的使用吧,代码看上去有些臃肿,不过解题思想很好,就是效率太低了。mark一下。
让我想到了大一时候学习递归时候用的方法,但那时根本没有想过执行效率的问题。
在此我写出四种解法。
至于递归方法,简洁,但效率就低多了去了。
本题不难,只是随便比较一下执行效率,高手请手下留情。
n取32,太大了方法四就堆溢出了,再大就超过int存储长度了。
上代码:
可以看出方法一直接递归效率是非常慢的,方法二和方法三在同一个数量级。至于方法四,我想说蓝桥杯上的方法也太水了吧。。。整整多了四个数量级。
让我想到了大一时候学习递归时候用的方法,但那时根本没有想过执行效率的问题。
在此我写出四种解法。
至于递归方法,简洁,但效率就低多了去了。
本题不难,只是随便比较一下执行效率,高手请手下留情。
n取32,太大了方法四就堆溢出了,再大就超过int存储长度了。
上代码:
import java.util.ArrayList; import java.util.List; class Cow { static int f[]=new int[1000]; //方案一:该年的母牛个数等于 前年的母牛个数(生一个小牛) 加上 去年的母牛个数 static int f(int n) { return n>2 ? f(n-2)+f(n-1) : 1; } //方案二:打表提高执行效率 static int f2(int n) { for(int i=1;i<n;i++) g(i); return g(n); } static int g(int n) { f = n>2 ? f[n-2]+f[n-1]:1; return f ; } //方案二:迭代节省空间同时提高执行效率 static int f3(int n) { int a,b,sum; a=b=sum=1; for(int i=3;i<=n;i++) { sum=a+b; a=b; b=sum; } return sum; } //方案四:直接模拟母牛生产过程 private int age; public Cow afterYear() { age++; return age > 2 ? new Cow() :null;//填空 } public static void f4(int n) { List<Cow> list = new ArrayList<Cow>(); list.add(new Cow()); for (int i = 0; i < n; i++) { int cowCount = list.size(); for (int j = 0; j < cowCount; j++) { Cow cow = list.get(j).afterYear(); if (cow != null) { cow.age++;// 填空 list.add(cow); } } } System.out.println(list.size()); } public static void main(String[] args) throws Exception { long start=System.nanoTime(); System.out.print("方案一:"); System.out.println(f(32)); System.out.println(System.nanoTime()-start); start=System.nanoTime(); System.out.print("方案二:"); System.out.println(f2(32)); System.out.println(System.nanoTime()-start); start=System.nanoTime(); System.out.print("方案三:"); System.out.println(f3(32)); System.out.println(System.nanoTime()-start); start=System.nanoTime(); System.out.print("方案四:"); f4(32); System.out.println(System.nanoTime()-start); } } //方案一:2178309 //10237153 //方案二:2178309 //59283 //方案三:2178309 //45040 //方案四:2178309 //473797733
可以看出方法一直接递归效率是非常慢的,方法二和方法三在同一个数量级。至于方法四,我想说蓝桥杯上的方法也太水了吧。。。整整多了四个数量级。
相关文章推荐
- 基因牛 张教授采用基因干预技术成功培养出一头母牛,三年后,这头母牛每年会生出1头母牛, 生出来的母牛三年后,又可以每年生出一头母牛。
- 一个java面试题:一个农夫养了一头牛,三年后,这头牛每年会生出一头牛,生出来的牛三年后又可以每年生出一头牛,不考虑牛的性别和生死,问农夫10年后有多少头牛?
- 有一母牛,到4岁可生育,每年一头,所生均是一样的母牛,到15岁绝育,不再能生,20岁死亡,问n年后有多少头牛
- 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
- 农场一头小母牛,每年生头小母牛,母牛5岁产母牛,20年上多少牛?--java面向对象方式实现
- 若一头小母牛,从出生起第四个年头开始每年生一头母牛,按此规律,第n年有多少头母牛?
- 有一头小母牛。从第四年起每年生一头小母牛。生的小母牛也是从第四年起每年生一头小母牛。如此循环。编程求N年后有多少头母牛。。。。。。。
- 设有一头小母牛,从出生第四年起每年生一头小母牛,按此规律,第N年时有几头母牛?
- 设有一头小母牛,从出生第四年起每年生一头小母牛,按此规律,第N年时有几头母牛?
- [转]C#算法 有一个农场有一头成年母牛,每三个月后一头小牛,小牛一年后长大,长大后每三个月又可以生一头小牛,如此循环,问n年后农场一共有多少牛?
- 母牛繁殖问题:一头母牛,每年年初生一头小母牛,每头小母牛从第四个年头起,每年年初也要生一头小母牛,问:第20个年头后共有多少只牛?
- C#算法 母牛从第4年起每年生一头小母牛,并且母牛不会死
- 一个农场有头母牛,现在母牛才一岁,要到四岁才能生小牛,四岁之后,每年生一头小牛,n年后有多少头小牛
- 面向对象设计题:“农场一头小母牛,每年生头小母牛。。。”的一种实现
- 牛年求牛:有一母牛,到4岁可生育,每年一头,所生均是一样的母牛,到15岁绝育,不再能生,20岁死亡,问n年后有多少头牛。
- 若一头小母牛,从出生起第四个年头开始每年生一头母牛,按此规律,第n年时有多少头母牛?
- 母牛生产问题;第一年有一头小母牛,3年之后每年都生一头小母牛,假设都不死,第n年共有多少母牛,输出n从1到20的母牛数目
- 程序设计题:农场一头小母牛_每年生头小母牛_面对象!
- 若一头小母牛,从出生起第四个年头开始每年生一头母牛,按此规律,第n年时有多少头母牛?
- 牛年求牛:有一母牛,到4岁可生育,每年一头,所生均是一样的母牛,到15岁绝育,不再能生,20岁...