兔子迭代问题
2016-12-21 14:11
113 查看
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
程序设计:
public class exp2{
public static void main(String args[]){
int i=0;
for(i=1;i<=20;i++)
System.out.println(f(i));
}
public static int f(int x)
{
if(x==1 || x==2)
return 1;
else
return f(x-1)+f(x-2);
}
}
或
public class exp2{
public static void main(String args[]){
int i=0;
math mymath = new math();
for(i=1;i<=20;i++)
System.out.println(mymath.f(i));
}
}
class math
{
public int f(int x)
{
if(x==1 || x==2)
return 1;
else
return f(x-1)+f(x-2);
}
}
程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
程序设计:
public class exp2{
public static void main(String args[]){
int i=0;
for(i=1;i<=20;i++)
System.out.println(f(i));
}
public static int f(int x)
{
if(x==1 || x==2)
return 1;
else
return f(x-1)+f(x-2);
}
}
或
public class exp2{
public static void main(String args[]){
int i=0;
math mymath = new math();
for(i=1;i<=20;i++)
System.out.println(mymath.f(i));
}
}
class math
{
public int f(int x)
{
if(x==1 || x==2)
return 1;
else
return f(x-1)+f(x-2);
}
}
相关文章推荐
- 每天一道算法--经典兔子繁殖迭代问题(斐波那契数列)
- LoadRunner HTTP脚本迭代处理的常见问题
- 狼追兔子问题的解答
- 用const_reverse_iterator迭代的问题
- 兔子的枚举取值的问题
- 经典问题--求兔子数量
- 爬楼梯问题迭代算法解!
- 兔子繁殖问题.
- 经典的100个C算法__(1)兔子问题
- 狐狸吃兔子问题算法
- 狐狸与兔子问题
- 旧文重发:产品线工程:团队迭代及其问题
- ArrayList的问题(迭代的时候不允许修改)
- 用const_reverse_iterator迭代的问题
- 旧文重发:产品线工程:团队迭代及其问题
- Struts迭代问题解决:如何在一行显示多个记录(同一字段)
- 算法习作:兔子生兔子问题 (-_-!)
- 迭代集合删除问题
- 数学递推思想在程序中的应用(一)----------兔子问题
- 数学趣题——兔子产仔问题