服务器之家

服务器之家 > 正文

Java编程计算兔子生兔子的问题

时间:2020-08-16 15:13     来源/作者:Java教程网

程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....

程序设计:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
  }
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
  }
}

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
返回顶部