斐波那契数列_兔子问题
2016-05-04 16:27
281 查看
package hhxy;
/*兔子问题
* @HF
* 分析 1 2 3 4 5 6
* 1 1 2 3 5 8 13 21
*/
public class Rabbit {
public static void main(String[] args) {
// Rabbit.rabbit(4);
System.out.println(Rabbit.rabbit2(45));
}
public static void rabbit(int month){
int t=1;
int t2=1;
int t3;
for(int i=3;i<month;i++){
t3=t+t2;
t=t2;
t2=t3;
System.out.println("第"+month+"共有"+t2+"只兔子");
}
}
//递归
public static int rabbit2(int month){
if(month==1 || month==2){
return 1;
}else{
return rabbit2(month-1)+rabbit2(month-2);
}
}
}
/*兔子问题
* @HF
* 分析 1 2 3 4 5 6
* 1 1 2 3 5 8 13 21
*/
public class Rabbit {
public static void main(String[] args) {
// Rabbit.rabbit(4);
System.out.println(Rabbit.rabbit2(45));
}
public static void rabbit(int month){
int t=1;
int t2=1;
int t3;
for(int i=3;i<month;i++){
t3=t+t2;
t=t2;
t2=t3;
System.out.println("第"+month+"共有"+t2+"只兔子");
}
}
//递归
public static int rabbit2(int month){
if(month==1 || month==2){
return 1;
}else{
return rabbit2(month-1)+rabbit2(month-2);
}
}
}
相关文章推荐
- FZU 1061 矩阵连乘
- 一个好产品,只是帮用户做好了一件事
- 基于Jcrop的图片上传裁剪加预览
- EditText----
- iOS 推送,当接到推送消息时如何处理?
- 如何设计MVP中的Presentation层
- facebook 图片加载框架
- 如何利用Cacti监控机房温度
- Giraph中有关Aggretor的处理过程
- 编程练习赛1:优化延迟
- 城市列表
- Memcached详解(较详细)
- 解决inno setup打包,执行权限导致无法执行问题
- linux 特殊权限的介绍
- 父类构造器
- http 缓存机制
- java中的String.split() 中“|”作为分隔符的问题和数组长度问题
- C# 动态修改Config
- 一致性哈希
- 【框架】CoordinatorLayout&RecyclerView 协调者布局+卡片布局