zoukankan      html  css  js  c++  java
  • 斐波那契数列

    斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”。  

    斐波那契当时是为了描述如下情况的兔子生长数目:
      第一个月初,有一对刚诞生的兔子
      第二个月之后(第三个月初),它们可以生育
      每月每对可生育的兔子会诞生下一对新兔子
      兔子永不死去
    

      可以用图片这样描述:

      上述数列是这样的:1、1、2、3、5、8、13、21、34、……

      斐波那契用数学上的函数这样定义上述数列:

      

    这就是大名鼎鼎的斐波那契数列。代码实现如下: 

    package com.itszt.test8;
    /**
     * 斐波那契数列
     */
    public class Test3 {
        static void f(int x){
            int f1=1,f2=1;
            if(x==1){
                System.out.println(f1);
            }
            if(x==2){
                System.out.println(f1+"  "+f2);
            }
            if(x>=3){
                for(int j=1;j<=x;j++){
                    System.out.print(j+"   ");
                }
                System.out.println();
                System.out.print(f1+"   "+f2);
                int i=3;
                while(i<=x){
                    f1=f1+f2;
                    f2=f1+f2;
                    System.out.print("   "+f1);
                    System.out.print("   "+f2);
                    i++;
                }
            }
        }
    
        public static void main(String[] args) {
            f(7);
        }
    }
    

      运行上述代码,结果如下:

    1   2   3   4   5   6   7   
    1   1   2   3   5   8   13   21   34   55   89   144  
  • 相关阅读:
    28. css样式中px转rem
    27.用webpack自搭react和vue框架
    26.webpack 入门
    25.redux回顾,redux中的action函数异步
    24.redux
    23.react-router 路由
    22.2、react生命周期与react脚手架(二)
    22.1 、react生命周期(一)
    21.react 组件通信
    const关键字的作用
  • 原文地址:https://www.cnblogs.com/lizhangyong/p/8111370.html
Copyright © 2011-2022 走看看