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

发布时间:2020-09-17 14:35:16 作者:mdxy-dxy
来源:脚本之家 阅读:133

程序分析: 兔子的规律为数列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. C语言解决关于兔子的古典问题的代码
  2. python怎么实现兔子生兔子示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

兔子生兔子 ava j

上一篇:python添加环境变量的方法

下一篇:如何在Ubuntu 的发行版上安装微软TrueType字体

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》