zoukankan      html  css  js  c++  java
  • Java版经典兔子繁殖迭代问题——斐波那契(Fibonacci)数列

    /**
     * 题目:
     * 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子。
     * 假如兔子都不死,问经过month个月后,兔子的总数为多少对?
     */
    public class Fibonacci {
    	
    	// 月份
    	static Integer month = 3; // 注意:month > 0
    
    	public static void main(String[] args) {
    		Integer pair = f(month);
    		System.out.println("答:经过" + month + "个月后,兔子的总数为" + pair + "对。");
    	}
    	
    	/**
    	 * f(n)=f(n-1)+f(n-2) (n>=3)
    	 * @param month
    	 * @return
    	 */
    	public static Integer f(Integer month){
    		if (month ==1 || month == 2) {
    			return 1;
    		}else {
    			return f(month - 1) + f(month - 2);
    		}
    	}
    }
    

      

     

  • 相关阅读:
    vim技巧2
    vim技巧1
    网站压力测试工具
    CentOS mysql安装
    破解root
    渐进式性能监测案例
    网络监测介绍
    I/O检测介绍
    虚拟内存介绍
    @Slf4j
  • 原文地址:https://www.cnblogs.com/GalaxyNote/p/5399123.html
Copyright © 2011-2022 走看看