zoukankan      html  css  js  c++  java
  • 斐波那契数列------兔子产仔问题1

    import java.util.Scanner;

    /*
    * 第一个月:1对兔子;
    * 第二个月:1对兔子;
    * 第三个月:2对兔子;
    * 第四个月:3对兔子;
    * 第五个月:5对兔子;
    * 从上述内容可以看出,从第3个月开始,每个月的兔子总对数等于前两个月兔子数的总和。
    * 第n个月兔子的总数fn=fn-2+fn-1
    * 这里,初始第一个月的兔子数为f1=1,第2个月的兔子数为f2=1.
    *
    *
    *
    */
    public class 兔子产仔问题斐波那契数列
    {
    public static int sum(int n)
    {
    int t1,t2;
    if(n==1||n==2)
    {
    return 1;
    }
    else
    {
    t1=sum(n-1);
    t2=sum(n-2);
    return t1+t2;
    }
    }
    public static void main(String[] args)
    {
    System.out.println("算法求兔子产仔问题");
    System.out.println("请先输入时间:");
    Scanner h=new Scanner(System.in);
    int n=h.nextInt();
    int num=sum(n);
    System.out.println("经过"+n+"月的时间,共繁殖成"+num+"对兔子!");

    }

    输出:

    算法求兔子产仔问题
    请先输入时间:
    12
    经过12月的时间,共繁殖成144对兔子!

  • 相关阅读:
    字典与集合
    gitee
    在使用pycharm时同时缩进、左移、多行注释
    代码1(while循环和IF条件语句,字符格式化,break,continue)
    python基础-工具
    11 Serializer组件
    10 响应模块
    09 异常模块
    08 解析模块
    07 渲染模块
  • 原文地址:https://www.cnblogs.com/fanzhengzheng/p/7667631.html
Copyright © 2011-2022 走看看