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);
    		}
    	}
    }
    

      

     

  • 相关阅读:
    杭电 1013 Digital Roots
    杭电 1040 As Easy As A+B 【排序】
    杭电 2092 整数解
    bzoj3223
    bzoj3224
    LA3905
    bzoj3601
    bzoj1002
    bzoj3105
    bzoj3332
  • 原文地址:https://www.cnblogs.com/GalaxyNote/p/5399123.html
Copyright © 2011-2022 走看看