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

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM