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

  

 

原文地址:https://www.cnblogs.com/GalaxyNote/p/5399123.html