有一对兔子,从出生后第3个月起每个月都生一对兔子, 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

解: 然我们看看 没个月兔子的总数量 1 1 2 3 5 8 13 21 34 。。。。。

靠,这就是一个Fibonacci的问题

java 实现 :

public class TestNIO {
	public static void main(String[] args) {
		fibo(7);
	}
	/**
	 * :有一对兔子,从出生后第3个月起每个月都生一对兔子,
	 * 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
	 *  0 1 1 2 3 5 8 13 21 ....
	 * @param month
	 */
	private static void fibo(int month) {
		// 从数据的裴烈规律可以看出 这是一个斐波那契数列的问题
		/// 定义数据 上个月的数量 上上个月的数量
		int pre = 1 ;
		int prepre = 1 ;
		/// 总数量
		int total ;
		if(month == 1){
			System.out.println("第一个月的数量 :"+1);
		}else if(month == 2){
			System.out.println("第一个月的数量 :"+1);
			System.out.println("第二个月的数量 : " + 1);
		}else if(month > 2){
			System.out.println("第1个月的数量 :"+1);
			System.out.println("第2个月的数量 : " + 1);
			for(int j = 3 ; j <= month ; j++ ){
				total = pre + prepre ;
				System.out.println("第" + j + "个月的总数量 :" + total);
				prepre = pre ;
				pre = total ;
			}
		}
	}
}
第1个月的数量 :1
第2个月的数量 : 1
第3个月的总数量 :2
第4个月的总数量 :3
第5个月的总数量 :5
第6个月的总数量 :8
第7个月的总数量 :13




猜你喜欢

转载自1208815066.iteye.com/blog/2040583
今日推荐